Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Show loading gif while Iframe loads

I need a loading GIF to show up while the iframe is loading.
This is what I came up with:

<div id="m-load" style="position:relative;top:45%;left:45%;">
<img src="images/loader.gif"/></div>
<iframe src="http://myserver.com/myfile.html" id="m-iframe"></iframe>

And in the iframe source file I entered:(myfile.html)

<head>
<script>
top.document.getElementById('m-load').style.display ="none";
</script>
</head>

But this won't work in Firefox (permission denied).
Any other way of achieving this?

like image 719
funerr Avatar asked Jun 19 '26 14:06

funerr


1 Answers

You need to do it on the same page using onload event which fires after all iframes are loaded like this:

// show loading image

window.onload = function(){
  // hide loading image
}

<img id="img" src="images/loader.gif"/></div>

<script>
window.onload = function(){
   document.getElementById('img').style.display = "none";
}
</script>
like image 189
Sarfraz Avatar answered Jun 21 '26 03:06

Sarfraz