最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically loading CSS - Stack Overflow

programmeradmin2浏览0评论

I'm trying to create a page theme function for my site. I want to load the corresponding themes dynamically on the page using separate CSS files.

I'm using this code:

  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", 'link.css')

  document.getElementsByTagName("head")[0].appendChild(fileref)

Which works fine, but it doesn't return any info if the CSS file has loaded or not.

Is there a way to catch when the .css is loaded? Maybe by using ajax?

I'm trying to create a page theme function for my site. I want to load the corresponding themes dynamically on the page using separate CSS files.

I'm using this code:

  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", 'link.css')

  document.getElementsByTagName("head")[0].appendChild(fileref)

Which works fine, but it doesn't return any info if the CSS file has loaded or not.

Is there a way to catch when the .css is loaded? Maybe by using ajax?

Share Improve this question edited Sep 19, 2010 at 2:21 Yi Jiang 50.1k16 gold badges138 silver badges136 bronze badges asked Sep 19, 2010 at 2:08 MoeMoe 3995 silver badges12 bronze badges 1
  • see useful link here cssnewbie.com/simple-jquery-stylesheet-switcher – Pramendra Gupta Commented Sep 19, 2010 at 2:21
Add a comment  | 

1 Answer 1

Reset to default 20

Internet Explorer will trigger an onReadyStateChange event when CSS file is loaded (or any other change in it's readyState). Other browsers do not trigger any event, so you will have to manually check if the stylesheet has been loaded, which is easily possible by checking the document.styleSheets object at a fixed interval.

Example

window.onload = function (){
    var filename = "link.css",sheet,i;
    var fileref = document.createElement("link");

    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", filename);

    readyfunc = function () {
        alert("File Loaded");
    }

    timerfunc = function (){
        for (i=0;i<document.styleSheets.length;i++){
            sheet = document.styleSheets[i].href;
            if(sheet !== null && sheet.substr(sheet.length-filename.length) == filename)
                return readyfunc();
        }
        setTimeout(timerfunc,50);  
    }

    if (document.all){ //Uses onreadystatechange for Internet Explorer
        fileref.attachEvent('onreadystatechange',function() {
            if(fileref.readyState == 'complete' || fileref.readyState == 'loaded')
                readyfunc();
        });
    } else {    //Checks if the stylesheet has been loaded every 50 ms for others
        setTimeout(timerfunc,50);
    }
    document.getElementsByTagName("head")[0].appendChild(fileref);    
}

It's ugly, but it works in all browsers.

发布评论

评论列表(0)

  1. 暂无评论