Use javascript to check http status codes

The keyword you’re missing is “status code” (that’s what we collectively call all the HTTP response codes of 200, 404, 500, etc). I’m going to assume you’re using jQuery, in which case, all the documentation you need for doing AJAX is at http://api.jquery.com/jQuery.ajax/

Here’s a simple example for a request that displays an alert, but only if a 404 status code is returned (lifted almost verbatim the link above):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
   $(function() {
      var url = "some_url";
      $.ajax(url,
      {
         statusCode: {
         404: function() {
            alert('page not found');
         }
      }
   });   
});
</script>

Leave a Comment