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.
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.
$(window). on('load', function () { $("#coverScreen"). hide(); }); Above solution will be fine whenever the page is loading.
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.
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();
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
}
});
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With