-
Notifications
You must be signed in to change notification settings - Fork 0
/
preloader.jquery.js
104 lines (95 loc) · 3.77 KB
/
preloader.jquery.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
/**
* A jQuery asset preloader. It preloads Images, CSS and HTML. It's useful for loading mini apps and widgets.
*
* Licensed under the WTFPL http://sam.zoy.org/wtfpl/
* Written By Greg Priday <http://siteorigin.com/>
*/
(function ($) {
$.preload = function (assets, options) {
options = $.extend({
'nocache':false, // Dont cache requests
'start':null, // Callback when preloading starts
'each':null, // Callback after each asset completes
'complete':null, // Callback after all assets complete
'insert':false, // Should JS and CSS be inserted after completion (per asset)?
'insertComplete':true // Should JS and CSS be inserted after completion of all assets?
}, options);
var isComplete = function () {
var c = 0;
for (var i = 0; i < assets.length; i++) {
if (assets[i].loaded) c++;
}
return c == assets.length;
}
// Preload all the assets
var assetLoad = function (i) {
var asset = assets[i];
if (!asset.loaded) {
if (asset.callback != null) assets[i].callback();
asset.loaded = true;
if (asset.insert && (asset.type == 'css' || asset.type == 'js')) {
asset.el.appendTo($('head'));
}
if (options.each == 'function') options.each(assets[i]);
if (isComplete()) {
if (typeof options.complete == 'function') options.complete(assets);
if (options.insertComplete) {
// Insert all assets
$.each(assets, function (i, asset) {
if (asset.type == 'css' || asset.type == 'js') {
asset.el.appendTo($('head'));
}
else if (asset.type == 'html' && asset.appendTo) {
$(asset.appendTo).append(asset.el);
}
})
}
}
}
};
$.each(assets, function (i, asset) {
asset = $.extend({
'type':null,
'callback':null // Callback after item is loaded
}, asset);
var preload = false;
var url = asset.url;
if(options.nocache){
// Add the nocache
url = url + (url.split('?') > 1 ? '&' : '?') + 'nocache=' + Math.random();
}
if (asset.type == 'image') {
asset.el = $('<img />')
.attr('src', url);
preload = true;
}
else if (asset.type == 'css') {
asset.el = $('<link rel="stylesheet" type="text/css" media="all" />')
.attr('href', url);
preload = true;
}
else if (asset.type == 'js') {
asset.el = $('<script type="text/javascript" />')
.attr('src', url);
preload = true;
}
else if (asset.type == 'html') {
$.get(url, function (data) {
assets[i].el = $(data);
assetLoad(i);
});
}
if (preload) {
// Load the asset through an image
assets[i] = asset;
var img = $('<img />')
.attr('src', asset.url)
.ready(function () {
assetLoad(i, asset)
});
}
});
if (options.start != null) options.start();
return assets;
}
})(jQuery);