addStyleRule fails when the link element is from a different domain #7

Closed
benbro opened this Issue Jan 7, 2013 · 1 comment

Comments

Projects
None yet
2 participants
Contributor

benbro commented Jan 7, 2013

addStyleRule tries to add css rules to the first link element.
If this element is loaded from a different domain, js can't add new rules to it.
I've solved it by creating a new link element.
The try/catch is not required any more.

var addStyleRule = (function () {
    var added = {};
    var styleElement = document.createElement('style');
    document.documentElement.getElementsByTagName('head')[0].appendChild(styleElement);
    var styleSheet = styleElement.sheet;

    return function (css) {
      if (added[css]) { return; }
      added[css] = true;
      styleSheet.insertRule(css, styleSheet.cssRules.length);
    };
  }());
Owner

timjb commented Jan 7, 2013

Great catch, thank you. I'll have a look at your other issue tomorrow.

timjb closed this in aa0fd6f Jan 7, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment