Proper jquery ajax loading image
I use this function on all my site ajax calls. My question is how would you rewrite it to have a proper pre-load image since mine is kind of a cheat and if the page takes too long to load or is 404 that loading image never goes away.
function ajaxcall(my_url, my_div, my_data)
{
$(my_div).append('<div style="text-align:center; position:absolute; top:0; left:0; width:100%;"><img src="/images/loading.gif" /></div>');
$.ajax({ url: my_url,
data: my_data,
type: 'get',
succ开发者_如何学Goess: function(output)
{
$(my_div).html(output);
}
});
}
Regards
You could use the error
option (see http://api.jquery.com/jQuery.ajax/) to write a handler function that deals with 404 or timeouts i.e removing the loading image and notifying the user somehow.
after that ajax call if it is a failure hide the div.
Otherwise you can put the image on another div.Initially it will be hide and in the start of function show the div and after ajax call hide the div whether it is success or failure.
精彩评论