Skip to content

Instantly share code, notes, and snippets.

@liddiard
Last active April 18, 2017 19:20
Show Gist options
  • Save liddiard/bd21b6962addf65e94d153c957f6d8fa to your computer and use it in GitHub Desktop.
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.
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