Anthony
Anthony

Reputation: 36008

show loading message while content loads

I know the following code would show a loading image when a button is clicked on the page:

   $.ajaxSetup ({  
         cache: false  
     });  
     var ajax_load = "<img src='img/load.gif' alt='loading...' />";  


     var loadUrl = "ajax/load.php";  
     $("#load_basic").click(function(){  
         $("#result").html(ajax_load).load(loadUrl);  
     });

but what if I want this to happen soon as the page loads. I dont want the user interaction of clicking a button to make the ajax call.

Upvotes: 0

Views: 2878

Answers (3)

mwgriffith
mwgriffith

Reputation: 550

You could try calling it from the $(document).ready() function. You might also want to check out this article, which adds a lot more to it.

Upvotes: 0

Praveen Prasad
Praveen Prasad

Reputation: 32137

jQuery(function(){
   // now you are triggering click your self not waiting for uer to click,
   //although he may click to start an ajax request
    $("#load_basic").trigger('click');
});

Upvotes: 0

Jason MacLean
Jason MacLean

Reputation: 513

Throw it inside the document ready function:

$(document).ready(function() {
$.ajaxSetup ({  
     cache: false  
 });  
 var ajax_load = "<img src='img/load.gif' alt='loading...' />";  
 var loadUrl = "ajax/load.php";   
 $("#result").html(ajax_load).load(loadUrl);  
}

Upvotes: 2

Related Questions