David Allen
David Allen

Reputation: 1163

Jquery - Check to see if page exists

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?

Upvotes: 2

Views: 9670

Answers (3)

S.Akruwala
S.Akruwala

Reputation: 1536

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.

Upvotes: 4

Lee
Lee

Reputation: 296

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");
    }
  }
});

Upvotes: 4

Royi Namir
Royi Namir

Reputation: 148714

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
  });

Upvotes: 3

Related Questions