1

I am checking URLs status codes in JavaScript. I am able to do it writing code to check status for one by one URL. But I have 100+ URLs which make my code too long if go for all 100+ URLs. Is there any way where I can pass a list of urls in JavaScript using .xls or .txt files which contains all URLs and iterate it.

Pankaj_Dwivedi
  • 565
  • 4
  • 15

1 Answers1

2

If you want to do it at frontend, I paste you some link to give you the direction.

the process is read text file ---> check url

  1. read text file

You can reference this question Reading a text file with jQuery [duplicate] to do the trick.

  1. check url

You can reference this question Checking if a URL is broken in Javascript.

So with these two reference question, I use a simple pseudo-code to tell you how to combine it and make it work.

function UrlExists(url, cb){
    jQuery.ajax({
        url:      url,
        dataType: 'text',
        type:     'GET',
        complete:  function(xhr){
            if(typeof cb === 'function')
               cb.apply(this, [xhr.status]);
        }
    });
}

$.get('YOUR_URL_FILE.txt', function(data) {
  // here I assume your data is this format:
  // url1
  // url2
  // url3
  var urls = data.split('\n');
  urls.forEach(function(url) {
    UrlExists(url, function(status){
      if(status === 200){
         // file was found
      }
      else if(status === 404){
         // 404 not found
      }
    });
  })
}, 'text');

Hope it helps you.

Chen-Tai
  • 3,435
  • 3
  • 21
  • 34