-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.getCSS.js
48 lines (44 loc) · 1.17 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
/*
* jQuery getCSS Plugin
* Copyright 2013, intesso
* MIT license.
*
* cross browser function to dynamically load an external css file.
* see: [github page](http://intesso.github.com/jquery-getCSS/)
*
*/
(function() {
/*
arguments: attributes
attributes can be a string: then it goes directly inside the href attribute.
e.g.: $.getCSS("fresh.css")
attributes can also be an objcet.
e.g.: $.getCSS({href:"cool.css", media:"print"})
or: $.getCSS({href:"/styles/forest.css", media:"screen"})
*/
var getCSS = function(attributes) {
// setting default attributes
if(typeof attributes === "string") {
var href = attributes;
attributes = {
href: href
};
}
if(!attributes.rel) {
attributes.rel = "stylesheet"
}
// appending the stylesheet
// no jQuery stuff here, just plain dom manipulations
var styleSheet = document.createElement("link");
for(var key in attributes) {
styleSheet.setAttribute(key, attributes[key]);
}
var head = document.getElementsByTagName("head")[0];
head.appendChild(styleSheet);
};
if(typeof jQuery === "undefined") {
window.getCSS = getCSS;
} else {
jQuery.getCSS = getCSS;
}
})();