image precaching

V

viza

Hi!

This does not seem to work:

function cacheimage(source){
myimg=new Image()
myimg.src=source
}
cacheimage('closed1.jpg')

Is this because the myimg object is destroyed as soon as the function
returns, or for some other reason? Does anyone have a simple, one argument,
void returning function with where you can give the url of an image and have
it precahed like this?

Any answer must work in IE5+.

Thanks!
 
L

Lasse Reichstein Nielsen

viza said:
This does not seem to work:

HOW does it not work? I.e., what did you expect to happen and what really
happens? And where do you put the code on a page?
function cacheimage(source){
myimg=new Image()
myimg.src=source
}
cacheimage('closed1.jpg')

Nothing obviously wrong with the code. I would put semicolons after the
statements for readability.
Is this because the myimg object is destroyed as soon as the function
returns,

No, mostly because it isn't destroyed. You are assigning the new image
to a global variable. Still, even local variables are usually not garbage
collected soon enough to prevent the image from being fetched
or for some other reason?

If it really doesn't work at all, it must be another reason.
Does anyone have a simple, one argument,
void returning function with where you can give the url of an image and have
it precahed like this?

Yours look perfectly fine to me. Until we can see how it fails, it is hard
to say anything else.

/L
 
J

Janwillem Borleffs

viza said:
Hi!

This does not seem to work:

function cacheimage(source){
myimg=new Image()
myimg.src=source
}
cacheimage('closed1.jpg')

Is this because the myimg object is destroyed as soon as the function
returns, or for some other reason? Does anyone have a simple, one argument,
void returning function with where you can give the url of an image and have
it precahed like this?

Any answer must work in IE5+.

Thanks!

Try this:

function cacheimage(source){
window['myimg']=new Image()
window['myimg'].src=source
}

This way, myimg will be a property of the window object, thus global.

Otherwise, although you do not the var declaration, myimg will be caught up
in the function's namespace only.


JW
 
J

Janwillem Borleffs

viza said:
and then Lasse Reichstein Nielsen said:


Try it:
http://homepage.ntlworld.com/l_vajzovic/testcase.html

It seems to be working in Moz at home, but IE5.0 at work seems to be not
doing it.

Which is quite amazing, because the file is incomplete. I have edited it
somewhat and now it works for me:

<html>
<head>
<title>testcase</title>
<script type="text/javascript">

function cacheimage(source){
myimg=new Image()
myimg.src=source
}
cacheimage('2.jpg')

window.onload = function () {
document.getElementById('imgid').onclick = function (){
this.src=myimg.src;
}
}

</script>
</head>
<body>
<p>Click on the image to change it - it should happen instantly.
<p style="background:#dddddd"><img alt="" src="1.jpg" id="imgid">
</body>
</html>


JW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top