Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a loading gif in a div until div loads content from other page

I am loading a page mypage.php and in div1 I include example.php from the same server.

The script is :

    <script>
$(document).ready(function(){

    $("#div1").load("example.php");
  });

</script>  

How can I add a loading.gif in while example.php is loading the content? I want to show the loading.gif only until the content loads.

like image 960
A.V. Avatar asked Dec 23 '14 17:12

A.V.


People also ask

How do I load a GIF while page is loading?

Using Code Step 1: Add loader DIV tag inside body tag. This DIV helps to display the message. Step 2: Add following CSS how it is going to displaying in browser. Step 3: Add following jQuery code when to fadeout loading image when page loads.

How do you show page loading Div until the page has finished loading?

$(window). on('load', function () { $("#coverScreen"). hide(); }); Above solution will be fine whenever the page is loading.

How can I make the browser wait to display the page until it's fully loaded?

To make the browser wait to display the page until it's fully loaded with JavaScript, we run our code in the window. onload method. to set the body element to have opacity 0 initially. window.


3 Answers

you have to set a img tag display: none like:

html:

<div id="img-load">
<img src="loading.gif" />
</div>

script:

loader = function(){
    $('#img-load').show();
    $( "#result" ).load( "example.php", function() {
      $('#img-load').hide();
    });
}
loader();
like image 101
miglio Avatar answered Oct 18 '22 05:10

miglio


try using like this:

$(document).ready(function(){
    $('#imageId').show();// imageId is id to your gif image div
    $('#div1').on('load','example.php',function(){
    $('#imageId').hide();// hide the image when example.php is loaded
  }
});
like image 39
Suchit kumar Avatar answered Oct 18 '22 05:10

Suchit kumar


This method worked for me:

HTML

<div id="load"><img src="loading_animation.gif" /></div>
<div id="content">Display content once loaded</div>

Javascript

<script type="text/javascript">
$(document).ready(function() {

   $('#load').show(); // Show loading animation
   $('#content').hide(); // Hide content until loaded

$(window).load(function() {

$.ajax({
  post: "GET",
  url: "your_file.php" // File that you're loading

}).done(function() {

  alert("Finished!"); // Alert message on success for debugging
  $('#load').hide(); // Hide loading animation
  $('#content').show(); // Show content

}).fail(function() {

  alert("Error!"); // Alert message if error for debugging

    });
  });
});
</script>
like image 21
Ulysnep Avatar answered Oct 18 '22 06:10

Ulysnep