LINK element and "onload" event



For a web app I am building, I am dynamically loading additional JavaScript and CSS files on demand (after the initial page load). According to the html specs, both the <script> and <link> elements support the onload event. I can get <script> tags to load and fire an onload event, but I am having issues with CSS link elements... Only IE seems to acknowledge a <link>'s onload event (since when does IE follow the rules??).

Here is some example code that works in IE, but fails in other browsers...

var link = document.createElement("link");
link.onload = function() {

link.type = "text/css";
link.rel = "stylesheet";
link.href = "somefile.css"; // change this to an actual CSS file


Any ideas about how to achieve a cross-browser compatible solution to this? Please don't question the reasons for such an implementation, this example is stripped down to the minimum for example purposes.

This article has been dead for over six months: Start a new discussion instead
Start New Discussion
Tags Related to this Article