Skip to content

Instantly share code, notes, and snippets.

@AMHOL
Last active December 20, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AMHOL/6053115 to your computer and use it in GitHub Desktop.
Save AMHOL/6053115 to your computer and use it in GitHub Desktop.
Normalize IE's createStyleSheet
if ( typeof document.createStyleSheet !== 'function' ) {
document.createStyleSheet = function(url, index) {
var styleElement;
if ( typeof url === 'string' ) {
styleElement = document.createElement('style');
if ( url.replace('{', '') !== url ) {
if ( styleElement.styleSheet ) {
styleElement.styleSheet.cssText = url;
} else {
styleElement.appendChild(document.createTextNode(url));
}
} else {
styleElement = document.createElement('link');
styleElement.rel = 'stylesheet';
styleElement.href = url;
}
} else {
styleElement = document.createElement('style');
}
styleElement.type = 'text/css';
if ( typeof index === 'number' ) {
if ( index > document.styleSheets.length - 1 ) {
index = document.styleSheets.length - 1;
}
if ( index < 0 ) {
index = 0;
}
var styleSheetTag = document.styleSheets[index].ownerNode;
styleSheetTag.parentNode.insertBefore(styleElement, styleSheetTag);
} else {
document.getElementsByTagName('head')[0].appendChild(styleElement);
index = document.styleSheets.length - 1;
}
try {
if ( !('addRule' in CSSStyleSheet.prototype) && typeof CSSStyleSheet.prototype.insertRule === 'function' ) {
CSSStyleSheet.prototype.addRule = function(selector, rule) {
this.insertRule(selector + '{ '+ rule + '}', this.cssRules.length);
};
}
} catch (e) {}
return document.styleSheets[index];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment