I noticed that stackoverflow has some JS code which generates: a warning with “Stack Overflow requires external JavaScript from another domain, which is blocked or failed to load.”. This happened to me a moment ago when Google Analytics didn’t load correctly.
How would I do something like this?
Note: I could have a but all other JS loads correctly this is only for google analytics which sometimes either never loads or takes forever.
When I check the code they have:
StackExchange.init = function () {
var e = function (a) {
if (!window.jQuery) if ("complete" != document.readyState) setTimeout(function () {
e(a)
}, 1E3);
else {
var d = document.createElement("div");
d.id = "noscript-padding";
var g = document.createElement("div");
g.id = "noscript-warning";
g.innerHTML = a + " requires external JavaScript from another domain, which is blocked or failed to load.";
document.body.insertBefore(d, document.body.firstChild);
document.body.appendChild(g)
}
But I am not sure what is going on with the setTimeout()
With
setTimeoutthey wait until the page is loaded completely. The functionecalls itself untildocument.readyStateiscompletewhere1E3is 1000 => 1 second:If you like to do the same for Google Analytics check out: javascript – Detect if Google Analytics is loaded yet?