Created
September 30, 2019 13:02
-
-
Save rajasajidmanzoor/206f75b8a262ba796d9c303205016b1f to your computer and use it in GitHub Desktop.
Check Broken Links in a Page JS/JQuery
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
| // 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