Div Load through Ajax and Jquery

I want to make a function where div will load silently with the help of ajax and jquery
I want if the link == 404 error then the alert will show “Page not Found” and if the internet not available then the alert show “Net Lost” when clicking on the link.
I created a code but I don’t know to implement the alert function as I request above.
Please help me on how to make the code.

//HTML
<li class="click me" rel="link" data-target = "dashboard.php">Dashboard</li>

$("li > rel[tab]").click(function() {
var mylink = $(this).attr('data-link');

   jQuery(".content").load(mylink,
        function(response, status, xhr) {
        //alert(response);     
    });
});

window.onload = function() {

   jQuery(".content").load('www.mydomain.com/interfacepages/default.php',
     function(response, status, xhr) {
        //alert(response);          

   });

}

Hi @developer62, you can get the status code from the xhr object:

$('.content').load(mylink, function (response, status, xhr) {
  if (xhr.status === 404) {
    // Show error message
  }
})

Please don’t use alert()s though as they are very obtrusive, and for that reason can get disabled altogether for your page.

You can check for navigator.onLine like so:

$('li > rel[tab]').click(function () {
  if (!window.navigator.onLine) {
    // Show error message and return
    return
  }

  // Otherwise proceed as usual
})
1 Like

so it should be like this in a correct manner -

$('li > rel[tab]').click(function () {
  if (!window.navigator.onLine) {
    alert("Net Lost");
    return false;
  } else {
    $('.content').load(mylink, function (response, status, xhr) {
       if (xhr.status === 404) {
         alert("Page Not Found");
         return false;
       }
    })
  }
})

? is this above right

Well after returning from the function the following code won’t get executed, so there is no need for an else block here… also why do you specifically return false? But anyway yes this should work, or does it not?

I nit understand sir. could you please update my code in full way ?

Closed, as the OP is no longer a member.

1 Like