link element onload

前端 未结 11 863
予麋鹿
予麋鹿 2020-11-30 03:31

Is there anyway to listen to the onload event for a element?

F.ex:

var link = document.createElement(\'link\');
link.rel =          


        
11条回答
  •  被撕碎了的回忆
    2020-11-30 04:24

    This trick is borrowed from the xLazyLoader jQuery plugin:

    var count = 0;
    
    (function(){
      try {
        link.sheet.cssRules;
      } catch (e) {
        if(count++ < 100)
          cssTimeout = setTimeout(arguments.callee, 20);
        else
          console.log('load failed (FF)');
        return;
      };
      if(link.sheet.cssRules && link.sheet.cssRules.length == 0) // fail in chrome?
        console.log('load failed (Webkit)');
      else
        console.log('loaded');
    })();
    

    Tested and working locally in FF (3.6.3) and Chrome (linux - 6.0.408.1 dev)

    Demo here (note that this won't work for cross-site css loading, as is done in the demo, under FF)

提交回复
热议问题