Reputation: 5464
I'm writing a page which is reliant on some advertising scripts, and if they are blocked the page just totally fails.
I need a way to detect whether or not external scripts successfully loaded -- if not, I'll force the page to continue without them.
Is there a way to do this programmatically? I have no control over the content of the ad scripts so I cant have them set a variable or something. A timeout is my first idea, but that seems messy (what if they just have a slow connection?). Further, listening for events on script elements is not reliable. any clean solutions to this issue?
Thank you
Upvotes: 1
Views: 1504
Reputation: 15004
A real low tech way would be for each script to put something in the global namespace. Then you can just check for the existence of that.
E.g.
advertisingScript1.js:
advertisingScript1Loaded = true;
...
now in your main page you can check to see if the script has loaded with:
if (typeof advertisingScript1Loaded !== 'undefined' && advertisingScript1Loaded) {
// Do something that relies on the script
...
}
Note if the scripts are set to load asynchronously you will need to have some kind of ready callback before you do the test.
Upvotes: 2