Skip to content

Instantly share code, notes, and snippets.

@valdrinkoshi
Last active January 19, 2017 01:25
Show Gist options
  • Save valdrinkoshi/4a92f97169a6fc41a1852f23211b8c4e to your computer and use it in GitHub Desktop.
Save valdrinkoshi/4a92f97169a6fc41a1852f23211b8c4e to your computer and use it in GitHub Desktop.
IE11/Edge cloned stylesheets not working
div { background-color: blue; color: white; }
<!DOCTYPE html>
<html>
<head>
<style id="myStyle"> div { background-color: red; } </style>
</head>
<body>
<div>I should have red background color, and after 1 second a white text color.</div>
<script>
setTimeout(function() {
// Link hosted in another node breaks the cascading order.
var div = document.createElement('div');
div.innerHTML = '<link rel="stylesheet" href="blue.css">';
var el = div.firstElementChild;
// Even if we clone it, it breaks the cascading order.
div.removeChild(el);
el = el.cloneNode(true);
// A new link w/o any parent works fine.
// Doesn't work for styles contained in <body>.
// el = document.createElement('link');
// el.setAttribute('rel', 'stylesheet');
// el.setAttribute('href', 'blue.css');
myStyle.parentNode.insertBefore(el, myStyle);
}, 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment