Created
May 29, 2018 23:27
-
-
Save Beej126/f26e0849b6b50de1731645d77b32a2bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//loop through all <script> tags and fire async XMLHttpRequest to report 404's | |
var scripts = document.getElementsByTagName("script"); | |
var i; | |
for (i = 0; i < scripts.length; i++) { | |
if (scripts[i].src === "") continue; | |
var xhr = new XMLHttpRequest(); | |
//closure on url so it's available when onreadystatechange fires later | |
//remember js needs a function block to closure | |
xhr.onreadystatechange = (function(url) { | |
return function() { | |
if (this.readyState === 4 && this.status === 404) { | |
console.error("bad script url: " + url); | |
var h3 = document.createElement("h3"); | |
h3.innerHTML = "bad url: " + url; | |
document.body.insertBefore(h3, document.body.firstChild); | |
} | |
}; | |
})(scripts[i].src); | |
xhr.open("GET", scripts[i].src, true); | |
xhr.send(null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment