0

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',
        success: function(output) 
            {
                $(my_div).html(output);
            }
    });
}

Regards

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.