Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwaitForImages.js
More file actions
Latest commit
63 lines (51 loc) · 2.3 KB
/
Copy pathwaitForImages.js
File metadata and controls
63 lines (51 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//function for executing a callback when all images in the jQuery object are loaded (or fail).
$.fn.waitForImages=function(callback){
var$images=this.find('img').add(this.filter('img')),
loaded=[],
temporary=[];
if($images.length<1){//no images to wait for, trigger immediately
setTimeout(callback,0);
return;
}
functionloadHandler(event){
//get a reference to the original image that this event is referring to
varimg=$(this).data('originalImage')||this;
if($.inArray(img,loaded)===-1){
loaded.push(img);
}
if(loaded.length>=$images.length){//all images are done, clean up and schedule the main callback
//remove all added event handlers on original img elements
$images.unbind('load error',loadHandler);
//remove all added event handlers on temporary img elements
for(vari=0;i<temporary.length;i++){
temporary[i].unbind('load error',loadHandler);
}
//DONE!!!
setTimeout(callback,0);
}
}
return$images
.bind('load error',loadHandler)
.each(function(i,el){//triggers the load event on images we already know are done
if(this.complete){//true in Chrome and FF for loaded and failed images; true in IE for loaded images ONLY
$(this).load();
}
elseif(this.readyState){//special cases for IE
if(this.src.lastIndexOf('/')===this.src.length-1){//src was set to the empty string; IE stores this as the window url without the filename
$(this).load();
}else{
//hack for getting an event triggered in IE so we can tell whether or not
//the image failed or if we're still waiting for it. We'll create another img element,
//attach an event handler to it, and give it the same src. If either the original or this
//temporary element fire an event, then we'll know what happened and for all purposes can assume the same thing
//happened to the other one. Notice that we don't need to add the temporary img element to the DOM.
var$tempImg=$('<img />');
temporary.push($tempImg);
$tempImg.bind('load error',loadHandler);
$tempImg.data('originalImage',this);//attach a reference to the original element to the temporary one; we'll use this
//later so we don't double count events
$tempImg.attr('src',this.src);
}
}
});
}