Hello,

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() {
  alert("yay!");
}

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

document.getElementsByTagName("head")[0].appendChild(link);

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.