[removed] capturing load event on LINK

后端 未结 9 1295
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 01:00

i\'m trying to attach an event handler to the load event of a link tag, to execute some code after a stylesheet has loaded.

new_element = document.createElem         


        
相关标签:
9条回答
  • 2020-11-29 01:38

    Even if you add an inline:

    <link rel="stylesheet" type="text/css" href="foo.css" onload="alert('xxx')"/>
    

    It won't fire in FireFox as there isn't an onload event for link elements. (It will work in IE)

    0 讨论(0)
  • 2020-11-29 01:38

    This function has held up on all browsers as well as in both cross domain and same domain situations, also this handles the injection of javascripts as well as stylesheets.

    function loadScript(src, type, callback_fn) {
        var loaded = false, scrpt, img;
        if(type === 'script') {
            scrpt = document.createElement('script');
            scrpt.setAttribute('type', 'text/javascript')
            scrpt.setAttribute('src', src);
    
        } else if(type === 'css') {
            scrpt = document.createElement('link')
            scrpt.setAttribute('rel', 'stylesheet')
            scrpt.setAttribute('type', 'text/css')
            scrpt.setAttribute('href', src);
        }
        document.getElementsByTagName('head')[0].appendChild(scrpt);
    
        scrpt.onreadystatechange = function(){
            if (this.readyState === 'complete' || this.readyState === 'loaded') {
                if(loaded === false) {
                    callback_fn();
                }
                loaded = true;
            }
        };
    
        scrpt.onload = function() {
            if(loaded === false) {
                callback_fn();
            }
            loaded = true;
        };
    
        img = document.createElement('img');
        img.onerror = function(){
            if(loaded === false) {
                callback_fn();
            }
            loaded = true;
        }
        img.src = src;
    };
    
    0 讨论(0)
  • 2020-11-29 01:41

    Nice shot Matt. I've created this helper with your comment.

    var CSSload = function(link, callback) {
        var cssLoaded = false;
        try{
            if ( link.sheet && link.sheet.cssRules.length > 0 ){
                cssLoaded = true;
            }else if ( link.styleSheet && link.styleSheet.cssText.length > 0 ){
                cssLoaded = true;
            }else if ( link.innerHTML && link.innerHTML.length > 0 ){
                cssLoaded = true;
            }
        }
        catch(ex){ }
        if ( cssLoaded ){
            callback();
        }else{
            setTimeout(function(){
                CSSload(link);
            }, 100);
        }
    };
    

    Usage:

    var $link = $('<link rel="stylesheet" media="screen" href="file.css"/>');
    $('head').append($link);
    CSSload($link.get(0), function(){
      // do something onLoad
    });
    
    0 讨论(0)
  • 2020-11-29 01:44

    Here's what is, in my opinion, a better solution for this issue that uses the IMG tag and its onerror event. This method will do the job without looping, doing contorted style observance, or loading files in iframes, etc. This solution fires correctly when the file is loads, and right away if the file is already cached (which is ironically better than how most DOM load events handle cached assets). Here's a post on my blog that explains the method - Back Alley Coder post - I just got tired of this not having a legit solution, enjoy!

    var loadCSS = function(url, callback){
        var link = document.createElement('link');
            link.type = 'text/css';
            link.rel = 'stylesheet';
            link.href = url;
    
        document.getElementsByTagName('head')[0].appendChild(link);
    
        var img = document.createElement('img');
            img.onerror = function(){
                if(callback) callback(link);
            }
            img.src = url;
    }
    
    0 讨论(0)
  • 2020-11-29 01:46

    The link.innerHTML, link.styleSheet and cssRules are all good approaches, but they do not work for stylesheets that belong to a domain outside of the origin domain (so cross-site stylesheet loading fails). This can be pretty unexpected when a subdomain vs a domain is used (www for example) or a static CNS is used. And its pretty annoying since elements have no same-origin restriction.

    Here's a solution that that uses the onload method for browsers that support it (IE and Opera), but then uses a timed interval for browsers that do not and compares the ownerNode and owningElement nodes to check to see when the stylesheet has made its way into the DOM.

    http://www.yearofmoo.com/2011/03/cross-browser-stylesheet-preloading.html

    0 讨论(0)
  • 2020-11-29 01:54

    For CSS stylesheets (not LINK elements in general) i'm using manual interval, by poking it's rules length. It works crossbrowser (AFAIT).

    try {
      if ( cssStylesheet.sheet && cssStylesheet.sheet.cssRules.length > 0 )
        cssLoaded = 1;
      else if ( cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText.length > 0 )
        cssLoaded = 1;
      else if ( cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0 )
        cssLoaded = 1;
    }
    catch(ex){}
    

    In code above, the cssStylesheet is DOMElement.

    0 讨论(0)
提交回复
热议问题