Detecting load of <link> resources?

前端 未结 1 604
梦如初夏
梦如初夏 2021-01-05 16:14

Browsers provide load events for

相关标签:
1条回答
  • 2021-01-05 16:59

    There may be a simpler way to do it, but this worked for me.

    Make sure your <link> tag has a title attribute:

    <link title="myStyles" rel="stylesheet" href="style.css" />
    

    Then use a function like this to check for the presence of a style within a particular styleseet:

    function linkLoaded(linkTitle, checkStyle)
    {
        for (var ix=0; ix<document.styleSheets.length; ix++) {
            try {
                if (document.styleSheets[ix].title == linkTitle) {
                    var mySheet=document.styleSheets[ix];
                    var myRules = mySheet.cssRules? mySheet.cssRules: mySheet.rules
                    for (var jx=0; jx<myRules.length; jx++) {
                        var thisSelector = myRules[jx].selectorText.toLowerCase();
                        if (thisSelector.substring(0, checkStyle.length) == checkStyle) {
                            alert("Found style!");
                            return;
                        }
                    }
                }
            }
            catch (err) {}    
        }
    
        alert("Not loaded!");
        return;
    }
    
    0 讨论(0)
提交回复
热议问题