Siva Ganesh
Siva Ganesh

Reputation: 45

Giving Alert Message in HTML if Url cannot be loaded

I'm using html and jquery. In my page I am opening an URL, if that URL is failed to open, 'Problem Load Page' or 'Server not found' error is displaying in webpage. How can I throw an Alert Box instead of showing error message webpage. Thanks in advance.

Upvotes: 0

Views: 371

Answers (1)

Davor Mlinaric
Davor Mlinaric

Reputation: 2027

you can check with ajax if page exists, and then redirect to it.

<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: 2

Related Questions