user867198
user867198

Reputation: 1648

get status of the requested URL added in <script src > tag

I have to perform async execution, and i have wrote the below code

var script = document.createElement('script');
script.src = document.getElementById('<%= hdnScannerUrl.ClientID %>').value;
document.getElementsByTagName('head')[0].appendChild(script);

Now i need to know whats the status of src like 404, 200 etc. Can i do that?

Note : i cannot use xmlhttprequest object or $.Ajax method, since they are not compatible with my request as they set the "referer" in the request header and the requested url (which is of device) will not response in this case.

Upvotes: 1

Views: 140

Answers (1)

epoch
epoch

Reputation: 16605

Lets say your module/logic inside the script is defined like this:

var module = {};

The when the onload event of the document is fired, you can check if the object exists.

window.onload = function() {
    if (module) {
        //script loaded
    }
}

There are probably some other ways, but this seems the easiest

UPDATE

Here is another way:

var script = document.createElement('script');
script.src = document.getElementById('<%= hdnScannerUrl.ClientID %>').value;
script.onload = function() { // note, use onreadystatechange for IE
    alert('i am loaded');
};

document.getElementsByTagName('head')[0].appendChild(script);

Upvotes: 1

Related Questions