2009
Apr
21
Dynamic Style Loading
Example
- var HTTPUTILITY = {
- getStyle: function(url, callback) {
- var isLoaded = false;
- var style = document.createElement("link");
- style.setAttribute("rel", "stylesheet");
- style.setAttribute("type", "text/css");
- style.setAttribute("href", url);
- style.onload = style.onreadystatechange = function() {
- if (!isLoaded && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
- isLoaded = true;
- if (typeof callback === "function") {
- callback();
- }
- if (this.tagName.toLowerCase() === "link") {
- document.getElementsByTagName("head")[0].removeChild(this);
- }
- }
- }
- var head = document.getElementsByTagName("head")[0];
- head.appendChild(style);
- }
- };
Example
- function complete() {
- alert("complete");
- }
- HTTPUTILITY.getStyle("myStyle.css", function(){complete();});
Related Post
- CSS Inclusion
回應 (Leave a comment)