skip to Main Content

I have an issue to making a function – load page without reloading, I have an problem that is “I want to show alert when link is 404 or not net connection is lost”. I know this code jqXHR.status == 0 to get net connection error or jqXHR.status !== 0 for 404 but I don’t know I don’t know how I implement this code with my existing code.

Please help me write this function.

$(function(){
    $("a[rel='tab']").click(function(e){
      pageurl = $(this).attr('href');
      $.ajax({url:pageurl+'?rel=tab',success: function(data){
        $('#containt').html(data);
      }});
      if(pageurl!=window.location){
        window.history.pushState({path:pageurl},'',pageurl);  
      }
      return false;  
    });
  });
  $(window).bind('popstate', function() {
    $.ajax({url:location.pathname+'?rel=tab',success: function(data){
      $('#containt').html(data);
    }});
  });
<a rel="tab" href="./404.php">Show me 404 ALert Pls</a>

2

Answers


  1. Add a callback for an "error" event or "statusCode".

    $.ajax({
          url:pageurl+'?rel=tab',
          success: function(data){
            $('#containt').html(data);
          },
      statusCode: {
        404: function() {
          alert( "page not found" );
        }
      }
    
    });
    

    From the docs:

    error

    Type: Function( jqXHR jqXHR, String textStatus, String
    errorThrown )

    A function to be called if the request fails. The
    function receives three arguments: The jqXHR (in jQuery 1.4.x,
    XMLHttpRequest) object, a string describing the type of error that
    occurred and an optional exception object, if one occurred. Possible
    values for the second argument (besides null) are "timeout", "error",
    "abort", and "parsererror". When an HTTP error occurs, errorThrown
    receives the textual portion of the HTTP status, such as "Not Found"
    or "Internal Server Error."
    (in HTTP/2 it may instead be an empty
    string) As of jQuery 1.5, the error setting can accept an array of
    functions. Each function will be called in turn. Note: This handler is
    not called for cross-domain script and cross-domain JSONP requests.
    This is an Ajax Event.

    statusCode (default: {})

    Type: PlainObject An object of numeric HTTP codes and functions to be
    called when the response has the corresponding code. If the request is
    successful, the status code functions take the same parameters as the
    success callback; if it results in an error (including 3xx redirect),
    they take the same parameters as the error callback.

    (version added: 1.5)

    Login or Signup to reply.
  2. you could do it this way using error function

        $.ajax({
            url: pageurl + '?rel=tab',
            error: function (xhr, ajaxOptions, thrownError) {
                if(xhr.status == 0){
                  alert('connect to internet');
                  return false; }
    
                alert(xhr.status);
                // here you can use the xhr code to return to main page or whatever
                alert(thrownError);
            },
            success: function (data) {
                $('#containt').html(data);
            }
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search