0

I have this code that I use to POST without reloading the page, but I wonder how I can add that in the script being executed display an image. example loader.gif

Thanks for your help.

<script language="javascript" src="jquery-1.6.4.min.js"></script>
<script language="javascript">
$(document).ready(function() {
    $().ajaxStart(function() {
        $('#loading').show();
        $('#result').hide();
    }).ajaxStop(function() {
        $('#loading').hide();
        $('#result').fadeIn('slow');
    });
    $('#form, #fat, #form').submit(function() {
        $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),
            success: function(data) {
                $('#result').html(data);

            }
        })

        return false;
    }); 
})  
  • possible duplicate of [Display loading image while post with ajax](http://stackoverflow.com/questions/2509711/display-loading-image-while-post-with-ajax) – js1568 Aug 27 '12 at 18:11

4 Answers4

0

Use JQuery to add the image to the page. Generally this is added close by the button the user clicks to start the AJAX request. This allows the user to see that something is happening in the background while your scripts run.

Alternatively, since your script is being run on document.ready, you could have the image from the get-go and then remove it once the ajax call completes.

thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
0

Not sure if I understand, but it seems you're quite near to it

// #loading could be an img tag pointing to loader.gif or a div containing the img
$('#loading').ajaxStart(function() {
    $(this).show();
    $('#result').hide();
}).ajaxStop(function() {
    $(this).hide();
    $('#result').fadeIn('slow');
});
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
0

put the loader in a hidden div (display:none) you can then use

$('#div').show();

before the ajax request and then hide it again in the success callback:

$('#div').hide();
SwiftD
  • 5,769
  • 6
  • 43
  • 67
0

at the first line of submit function:

$('#loadingImg').show();

and at the first line of callback success function:

$('#loadingImg').hide();
Dariush Jafari
  • 5,223
  • 7
  • 42
  • 71