Skip to content

Instantly share code, notes, and snippets.

@rajasajidmanzoor
Created September 30, 2019 13:02
Show Gist options
  • Select an option

  • Save rajasajidmanzoor/206f75b8a262ba796d9c303205016b1f to your computer and use it in GitHub Desktop.

Select an option

Save rajasajidmanzoor/206f75b8a262ba796d9c303205016b1f to your computer and use it in GitHub Desktop.
Check Broken Links in a Page JS/JQuery
// Copy & paste below code in browser console.
function urlExists(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
callback(xhr.status < 400);
}
};
xhr.open('HEAD', url);
xhr.send();
}
jQuery('a').each(function(e){
var el = jQuery(this);
var someUrl = el.attr('href');
var url_exists = true;
urlExists(someUrl, function(exists) {
console.log('"%s" exists?', someUrl, exists);
if(exists == false){
el.css('background', 'yellow');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment