开发者

Load image... when time is eating up

开发者 https://www.devze.com 2023-04-11 07:52 出处:网络
here is a sameple code, I would l开发者_JAVA百科ike to know when the browser is REALLY loading image

here is a sameple code, I would l开发者_JAVA百科ike to know when the browser is REALLY loading image

when you assign an image path to a array like that

imageNames[0] = 'image1.jpg';

or when you make

myImage = new Image();
myImage.src = imageNames[0];

i have put some timer.. but did not get concluant result ! HELP


You would look at the load event. Attach it with the ancient onload or addEventListener()/attachEvent() depending on your browser support requirements.

myImage = new Image;

myImage.onload = function() {
   alert('Image loaded');
}

myImage.src = imageNames[0];

jsFiddle.

You could also check if the image is already loaded by checking the complete property.


The key thing to note is that just because a human being can see that something could be a resource identifier / locator, the computer cannot.

When you assign a string to a point in an array:

imageNames[0] = 'image1.jpg';

the fact that 'image1.jpg' is a licit path to an image on your.host.net is not something the browser can determine on its own - the browser cannot recognize that the string 'image1.jpg' is a resource representation rather than a resource itself.

Once you set a DOM image's src property to be the string 'image1.jpg' the browser can recognize the string should be treated as a resource identifier (because the DOM element you created is an image, and the image's src property is supposed to be a URI pointing at an image resource which can be accessed and downloaded) and the browser will try and acquire that resource through means of its own.


The browser downloads the image when you assign a URL to the src attribute of an img element. Try this in the console of your browser and watch the Network tab:

var img = document.createElement('img');
img.src = 'foo';

You'll see network activity as soon as that second line executes.

It most certainly does nothing for imageNames[0] = 'image1.jpg'; since a string in an array could be anything, there's no way the browser has any idea this is supposed to be the URL of an image.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号