Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save zhuzhuaicoding/8051945 to your computer and use it in GitHub Desktop.

Select an option

Save zhuzhuaicoding/8051945 to your computer and use it in GitHub Desktop.
insertCSSLink: function (url, opts, callback) {
var sid, doc, t, cssLink, head, docMode = document.documentMode;
if (typeof opts == "string") {
sid = opts;
}
opts = typeof opts == "object" ? opts : {};
sid = opts.linkID || sid;
doc = opts.doc || document;
head = doc.getElementsByTagName("head")[0];
cssLink = ((t = doc.getElementById(sid)) && (t.nodeName == "LINK")) ? t : null;
if (!cssLink) {
cssLink = doc.createElement("link");
sid && (cssLink.id = sid);
cssLink.rel = cssLink.rev = "stylesheet";
cssLink.type = "text/css";
cssLink.media = opts.media || "screen";
head.appendChild(cssLink);
}
if (ua.ie || ua.opera) {
MUSIC.event.addEvent(cssLink, ((ua.ie && ua.ie < 9) ? "readystatechange" : "load"), function () {
if (!cssLink || ua.ie && ua.ie < 9 && !(cssLink.readyState == 'loaded' || cssLink.readyState == 'complete')) {
return;
}
if (callback) {
callback();
}
});
setTimeout(function () {
url && (cssLink.href = url);
}, 0);
} else if (ua.webkit) {
var cn = document.styleSheets.length;
url && (cssLink.href = url);
var _times = 10;
var fi = setInterval(function () {
if (document.styleSheets.length > cn) {
if (callback) {
callback();
}
clearInterval(fi);
} else {
_times--;
if (_times < 0) {
if (callback) {
callback();
}
clearInterval(fi);
}
}
}, 50);
} else if (ua.firefox) {
url && (cssLink.href = url);
var _times = 10;
var fi = setInterval(function () {
try {
cssLink.sheet.cssRules;
if (callback) {
callback();
}
clearInterval(fi);
} catch (e) {
_times--;
if (_times < 0) {
if (callback) {
callback();
}
clearInterval(fi);
}
}
}, 50);
} else {
if (callback) {
callback();
}
}
return(MUSIC.userAgent.ie != 9 && cssLink.sheet) || cssLink;
}, insertStyleSheet: function (sheetId, rules) {
var node = document.createElement("style");
node.type = 'text/css';
sheetId && (node.id = sheetId);
document.getElementsByTagName("head")[0].appendChild(node);
if (rules) {
if (node.styleSheet) {
node.styleSheet.cssText = rules;
} else {
node.appendChild(document.createTextNode(rules));
}
}
return node.sheet || node;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment