-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.getCSS.js
122 lines (101 loc) · 3.34 KB
/
jquery.getCSS.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
/**
* jQuery.getCSS plugin
* http://github.com/furf/jquery-getCSS
*
* Copyright 2010, Dave Furfero
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Inspired by Julian Aubourg's Dominoes
* http://code.google.com/p/javascript-dominoes/
*/
(function (window, document, jQuery) {
var head = document.getElementsByTagName('head')[0],
loadedCompleteRegExp = /loaded|complete/,
callbacks = {},
callbacksNb = 0,
timer;
jQuery.getCSS = function (url, options, callback) {
if (jQuery.isFunction(options)) {
callback = options;
options = {};
}
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.media = options.media || 'screen';
link.href = url;
if (options.charset) {
link.charset = options.charset;
}
if (options.title) {
callback = (function (callback) {
return function () {
link.title = options.title;
callback(link, "success");
};
})(callback);
}
// onreadystatechange
if (link.readyState) {
link.onreadystatechange = function () {
if (loadedCompleteRegExp.test(link.readyState)) {
link.onreadystatechange = null;
callback(link, "success");
}
};
// If onload is available, use it
} else if (link.onload === null /* exclude Webkit => */ && link.all) {
link.onload = function () {
link.onload = null;
callback(link, "success");
};
// In any other browser, we poll
} else {
callbacks[link.href] = function () {
callback(link, "success");
};
if (!callbacksNb++) {
// poll(cssPollFunction);
timer = window.setInterval(function () {
var callback,
stylesheet,
stylesheets = document.styleSheets,
href,
i = stylesheets.length;
while (i--) {
stylesheet = stylesheets[i];
if ((href = stylesheet.href) && (callback = callbacks[href])) {
try {
// We store so that minifiers don't remove the code
callback.r = stylesheet.cssRules;
// Webkit:
// Webkit browsers don't create the stylesheet object
// before the link has been loaded.
// When requesting rules for crossDomain links
// they simply return nothing (no exception thrown)
// Gecko:
// NS_ERROR_DOM_INVALID_ACCESS_ERR thrown if the stylesheet is not loaded
// If the stylesheet is loaded:
// * no error thrown for same-domain
// * NS_ERROR_DOM_SECURITY_ERR thrown for cross-domain
throw 'SECURITY';
} catch(e) {
// Gecko: catch NS_ERROR_DOM_SECURITY_ERR
// Webkit: catch SECURITY
if (/SECURITY/.test(e)) {
// setTimeout(callback, 0);
callback(link, "success");
delete callbacks[href];
if (!--callbacksNb) {
timer = window.clearInterval(timer);
}
}
}
}
}
}, 13);
}
}
head.appendChild(link);
};
})(this, this.document, this.jQuery);