Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting load of <link> resources?

Browsers provide load events for <script> and <img> tags. Is there a way to detect whether a request to a element has completed?

Specifically, I'm wishing to detect when a <link>'d stylesheet has been loaded.

Unfortunately, I think using a sentinel style and detecting load from a computedStyle isn't workable in my situation.

like image 879
Jeremy Dunck Avatar asked Apr 28 '09 00:04

Jeremy Dunck


People also ask

What is a URI example?

A URI — short for “Uniform Resource Identifier” — is a sequence of characters that distinguishes one resource from another. For example, foo://example.com:8042/over/there?name=ferret#nose is a URI containing a scheme name, authority, path, query and fragment. A URI does not need to contain all these components.

Which HTTP request header is used to identify the URL of the resource from which a request URL was obtained?

Referer: This optional header field allows the client to specify, for the server's benefit, the address ( URI ) of the document (or element within the document) from which the URI in the request was obtained.

What is the resource in a URL?

The target of an HTTP request is called a "resource", whose nature isn't defined further; it can be a document, a photo, or anything else. Each resource is identified by a Uniform Resource Identifier (URI) used throughout HTTP for identifying resources.

How do you find URI?

To find the project's service relative URI, look at the Create a REST service page. The service relative URI is also shown on the REST Resource URI Editor page.


1 Answers

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;
}
like image 51
Steven Richards Avatar answered Oct 19 '22 15:10

Steven Richards