0

I am trying to find out the broken links from the page but the issue is loop checks only for the last URL.(only with Javascript)

links = document.links;
for (var i = 0; i < links.length; i++) {
  var url_get = links[i].href;
  alert(url_get);
  var request = new XMLHttpRequest();
  request.open('get', url_get, true);
  request.onreadystatechange = check_urls; 
  function check_urls() {
    if (request.readyState === 4) {
       if ((request.status == 200) || (request.status == 0)) {
         alert(url_get + ' page is working');
       } else {
          alert(url_get + ' not working');
         return;
      }
    }
  }
 request.send(null);
}

It needs to be popup for every URL like it's working or not but its showing the result for only the last one.

Devs
  • 7
  • 1
  • 1
    what's the point of the or with status == 0 ? 404 is the standard code for not found; not getting what you're trying to do – CptKicks Sep 06 '19 at 04:24
  • Possible duplicate of [Check if image exists on server using JavaScript](https://stackoverflow.com/questions/18837735/check-if-image-exists-on-server-using-javascript) – Teemu Sep 06 '19 at 04:36
  • Notice, that any AJAX method can check links only on your own server, unless the linked server allow CORS. – Teemu Sep 06 '19 at 04:47

0 Answers0