Last active
April 18, 2017 19:20
-
-
Save liddiard/bd21b6962addf65e94d153c957f6d8fa to your computer and use it in GitHub Desktop.
Get a list of broken links on a page that match a given selector. Uses Fetch API. Assumes cross-origin requests, if any, are allowed by the server.
This file contains hidden or 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
function linkReport(selector) { | |
'use strict'; | |
const fetchOptions = { | |
method: 'HEAD' | |
}; | |
window.brokenLinks = []; | |
document | |
.querySelectorAll(selector) | |
.forEach(link => { | |
fetch(link.href) | |
.then(response => { | |
if (!response.ok) { | |
brokenLinks.push({ href: link.href, text: link.text }); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment