views:

286

answers:

2

I know there are thousands of examples on the internet, but I want for the script I already have to display a loading gif image while the data is retrievedd. My java knowledge are poor, therefore I'm asking how to change the following:

<script type="text/javascript"> 
 $(document).ready(function(){
  function getData(p){
    var page=p;
    $.ajax({
        url: "loadData.php?id=<? echo $id; ?>",
        type: "POST",
        cache: false,
        data: "&page="+ page,
        success : function(html){
            $(".content").html(html);
        }
    });
}
getData(1);

$(".page").live("click", function(){
    var id = $(this).attr("class");
    getData(id.substr(8));
        });
      });
  </script> 

And my div is here:

    <div class="content" id="data"></div>

Thanks. John

+1  A: 

Take a look at ajaxStart and ajaxStop

Felipe Lima
Felipe, thanks for the suggestion. However, I forgot to mention my js knowledge are poor? I saw those pages but don't know how to change my script.
DonJoe
+2  A: 

Let's say you have a tag someplace on the page which contains your loading message:

<div id='loadingmessage' style='display:none'>
  <img src='loadinggraphic.gif'/>
</div>

You can add two lines to your ajax call:

function getData(p){
    var page=p;
    $('#loadingmessage').show();  // show the loading message.
    $.ajax({
        url: "loadData.php?id=<? echo $id; ?>",
        type: "POST",
        cache: false,
        data: "&page="+ page,
        success : function(html){
            $(".content").html(html);
            $('#loadingmessage').hide(); // hide the loading message
        }
    });
Keltex
Dude, thanks a lot! It's perfect :)
DonJoe