-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjquery.preloader.js
176 lines (172 loc) · 6.4 KB
/
jquery.preloader.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* jQuery.preloader - v0.97.2 - K Reeve aka BinaryKitten
*
* Preloads a list of images, or all css linked images or both
* $.preLoadImages(array imageList,fn callback)
* imageList The array of image locations to preload
* callback a link or anonymous function that will process when all images are preloaded
*
* $.preLoadCssImages(fn callback)
* callback a link or anonymous function that will process when all images are preloaded
*
* $.preLoadAllImages(array imageList,fn callback)
* imageList The array of image locations to preload
* callback a link or anonymous function that will process when all images are preloaded
*
*
* ------------ Version History -----------------------------------
* v0.97.2
* fix for naming conventions on css image preloader - Thanks Bruno Couto
*
* v0.97.1
* fixed accidentally removed img setting line.
*
* v0.97
* Attempted fix for @import and @font-face
*
* v0.96.1
* Cleaned up Tabulation
*
* v0.96
* Thanks to the following people for their comments and fixes:
* Evgeni Nabokov
* Matt Fordham
* fanton
* Bruno Couto
*
* Adjusted the regex so that:
* - it will not match Quotes within the url
* - it does not match data uris which need no preloading
* Added in a few addition checks and clean ups
*
* v0.95
* # Note - keeping below v1 as really not sure that I consider it public usable.
* # But it saying that it does the job it was intended to do.
* Added Completion of loading callback.
* Main Reworking With Thanks to Remy Sharp of jQuery for Designers
*
*
* v0.9
* Fixed .toString being .toSteing
*
* v0.8
* Fixed sheet.href being null error (was causing issues in FF3RC1)
*
* v0.7
* Remade the preLoadImages from jQuery to DOM
*
* v0.6
* Fixed IE6 Compatability!
* Moved from jQuery to DOM
*
* v0.5
* Shifted the additionalimages loader in the preLoadAllImages so it wasn't called multiple times
* Created secondary .preLoadImages to handle additionalimages and so it can be called on itself
*/
(function ($) {
$.preLoadImages = function(imageList,callback) {
var pic = [], i, total, loaded = 0;
if (typeof imageList != 'undefined') {
if ($.isArray(imageList)) {
total = imageList.length; // used later
for (i=0; i < total; i++) {
pic[i] = new Image();
pic[i].onload = function() {
loaded++; // should never hit a race condition due to JS's non-threaded nature
if (loaded == total) {
if ($.isFunction(callback)) {
callback();
}
}
};
pic[i].src = imageList[i];
}
} else {
pic[0] = new Image();
if ($.isFunction(callback)) {
pic[0].onload = callback;
}
pic[0].src = imageList;
}
} else if ($.isFunction(callback)) {
//nothing passed but we have a callback.. so run this now
//thanks to Evgeni Nobokov
callback();
}
pic = undefined;
};
$.preLoadCSSImages = function(callback) {
var pic = [], i, imageList = [], loaded = 0, total, regex = /url\((?:"|')?(?!data:)([^)"']+)(?:"|')?\)/i,spl;
var cssSheets = document.styleSheets, path,myRules='',Rule,match,txt,img,sheetIdx,ruleIdx;
for (sheetIdx=0;sheetIdx < cssSheets.length;sheetIdx++){
var sheet = cssSheets[sheetIdx];
if (typeof sheet.href == 'string' && sheet.href.length > 0) {
spl = sheet.href.split('/');spl.pop();path = spl.join('/')+'/';
} else {
path = './';
}
if (sheet.cssRules) {
myRules = sheet.cssRules;
} else if (sheet.rules) {
myRules = sheet.rules;
}
if (myRules.length > 0) {
for (ruleIdx=0;ruleIdx<myRules.length;ruleIdx++) {
Rule = myRules[ruleIdx];
txt = Rule.cssText ? Rule.cssText : Rule.style.cssText;
txt = $.trim(txt);
if ('@' === txt.substr(0,1)) {
continue;
}
match = regex.exec(txt);
if (match != null) {
img = match[1];
if (img.substring(0,4) == 'http') {
imageList[imageList.length] = img;
} else if ( match[1].substring(1,2) == '/') {
var p2 = path.split('/');p2.pop();p2.pop();p2x = p2.join("/");
imageList[imageList.length] = p2x+img;
} else {
imageList[imageList.length] = path+img;
}
}
};
}
};
total = imageList.length; // used later
if (total > 0) {
for (i=0; i < total; i++) {
pic[i] = new Image();
pic[i].onload = function() {
loaded++; // should never hit a race condition due to JS's non-threaded nature
if (loaded == total) {
if ($.isFunction(callback)) {
callback();
}
}
};
pic[i].src = imageList[i];
}
} else if($.isFunction(callback)) {
//nothing found, but we have a callback.. so run this now
//thanks to Evgeni Nobokov
callback();
}
};
$.preLoadCssImages = $.preLoadCSSImages;
$.preLoadAllImages = function(imageList,callback) {
if (typeof imageList != 'undefined') {
if ($.isFunction(imageList)) {
callback = imageList;
} else if (!$.isArray(imageList)) {
imageList = [imageList];
}
}
$.preLoadCSSImages(function(){
if (imageList.length > 0) {
$.preLoadImages(imageList,callback);
} else {
callback();
}
});
}
})(jQuery);