Created
October 22, 2011 16:06
-
-
Save DavidBruant/1306146 to your computer and use it in GitHub Desktop.
Link checker
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
var links = document.getElementsByTagName('a'); | |
console.log(links.length); | |
var workingUrls = [], | |
brokenUrls = []; | |
function l(e){ | |
var t = e.target; | |
var url = t.src; | |
console.log(url, e.type); | |
// cleaning the DOM. | |
t.parentNode.removeChild(t); | |
switch(e.type){ | |
case 'load': | |
workingUrls.push(url); | |
break; | |
case 'error': | |
brokenUrls.push(url); | |
break; | |
default: | |
throw "Oopsy, wrong error type: "+e.type; | |
} | |
} | |
Array.prototype.forEach.call(links, function(e,i){ | |
if(i >= 5) | |
return; | |
var url = e.href; | |
var iframe = document.createElement('iframe'); | |
iframe.src = url; | |
//iframe.style = 'display:none'; | |
document.body.appendChild(iframe); | |
iframe.addEventListener('load', l, false); | |
iframe.addEventListener('error', l, false); | |
}); | |
setTimeout(function(){ | |
console.log(workingUrls); | |
console.log(brokenUrls); | |
}, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment