tags:

views:

26

answers:

2

hey guys ,

i know how to load a page in jquery and somehow i know how to use success but what if that page failed

im looking for a way to show loader image before the page loaded and if the page failed show a message

this is the function i wrote :

$(document).ready(function(){

    $("#loading").html("<img src='images/preload.gif' />");
    $(".thumb").load("stat.html");

});

now i want to change it in a way it shows preloader and the message if failed

A: 

Try using $.ajax instead of post so that you will have more control on the lifecycle using callback methods. In your case, you can use beforeSend, error, complete callbacks (which are self explanatory)

$.ajax({
   type: "POST",
   url: "stat.html",
   data: "name=John&location=Boston",
   beforeSend: function(){
        $("#loading").html("<img src='images/preload.gif' />");
   },
   success: function(msg){
     alert( "Data Saved: " + msg );
   },
   error: function(e){alert(e);}
 });
Teja Kantamneni
check my updates
Teja Kantamneni
+2  A: 

The load function has a calledback that provides the status of the request. It looks something like this...

$(selector).load(url,[],[callbackFn(response,success,xhr)]);

So you can do something like this:

$(".thumb").load("stat.html",function(response,status,xhr){
  if(status == "error") {
    //Something went wrong, have your error fallback code here
  }
});

You can find an example here: http://jsbin.com/uxisa3/2/edit

RussellUresti