1

I have a Admin panel(I am creating) with a list of URLs. I would like create script which can check to see if the urls is currently working or goes to a 404 page. Is this possible?

David Allen
  • 1,085
  • 2
  • 10
  • 22

3 Answers3

4

You can do this:

<a href="/somelink" id="test1">Link1</a> <span id="result1"></span>
$.ajax($("#test1").attr("href"), {
  statusCode: {
    404: function() {
      $("#result1").html("not working");
    },
    200: function() {
      $("#result1").html("working");
    }
  }
});
Lee
  • 286
  • 1
  • 16
3

Ajax calls to Get/Head will work only if the URL to check belongs to same site. If you are trying to check for the URL which belongs to some other site, then you will get Cross-site reference error.

In that case , you need check this at server side. Create an ajax call to server side method. At server side , create HttpRequest for the url and check its HttpResponse and reply back to ajax call.

S.Akruwala
  • 1,278
  • 11
  • 8
3

try this : (assuming the page are in the same domain)

  $.ajax(
  {
      type: "get",
      url: 'page.aspx',
      cache: false,
      statusCode: {
                    404: function ()
                       {
                          alert('page not found');
                       }
                   },
      async: true
  });
Royi Namir
  • 138,711
  • 129
  • 435
  • 755