Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript problem with iframe that's hidden before loaded

I have a page that contains an iframe that gets loaded using Javascript:

index.html

<iframe id="myFrame" width="800" height="600" style="display: none;"></iframe>
<div id="loader"><!-- some loading indicator --></div>

<script type="text/javascript">
function someFunction() {
  var myFrame = document.getElementById('myFrame');
  var loader = document.getElementById('loader');

  loader.style.display = 'block';

  myFrame.src = 'myFrame.html';
  myFrame.onload = function() {
    myFrame.style.display = 'block';
    loader.style.display = 'none';
  };
}
</script>

The page that gets loaded in the iframe contains some Javascript logic which calculates the sizes of certain elements for the purposes of adding a JS driven scrollbar (jScrollPane + jQuery Dimensions).

myFrame.html

<div id="scrollingElement" style="overflow: auto;">
  <div id="several"></div>
  <div id="child"></div>
  <div id="elements"></div>
</div>

<script type="text/javascript">
$(document).load(function() {
  $('#scrollingElement').jScrollPane();
});
</script>

This works in Chrome (and probably other Webkit browsers), but fails in Firefox and IE because at the time jScrollPane gets called, all the elements are still invisble and jQuery Dimensions is unable to determine any element's dimensions.

Is there a way to make sure my iframe is visible before $(document).ready(...) gets called? Other than using setTimeout to delay jScrollPane, which is something I definitely want to avoid.

like image 946
Aistina Avatar asked Apr 28 '10 13:04

Aistina


People also ask

Can iframe be hidden?

The hidden attribute hides the <iframe> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <iframe> is not visible, but maintains its position on the page.

Does iframe load if display none?

No, the iFrame's loading and load detection should not be compromised by setting the display to none. CSS is just for style, it has no ability to affect the DOM. Setting display:none on an <img> (or any of its parents) will prevent it from loading.

Why is iframe deprecated?

IFrames are not obsolete, but the reasons for using them are rare. Using IFrames to serve your own content creates a "wall" around accessing the content in that area. For crawlers like Google, It's not immediately clear that cotent in an iframe will be ranked as highly as if the content were simply part of the page.

Are IFrames render blocking?

No, it won't block — the contents of the frame will load asynchronously, like almost any other kind of content would.


2 Answers

Some browsers assume that when "display:none" is applied to replaced elements (like Flash or an iframe) the visual info for that element is no longer needed. So, if the element is later displayed by the CSS, the browser will actually recreate the visual data form scratch.

I imagine that having the iframe default to "display:none;" makes the browser skip the rendering of the HTML so the tags don't have any dimensions. I would set the visibility to "hidden" or position it off the page rather than use "display:none;".

Good luck.

like image 51
Steven P. Avatar answered Sep 18 '22 12:09

Steven P.


instead of making the iframe invisible by using display:none, you could try to...

... set visibility:hidden

... set position:absolute; top:-600px;

... set opacity:0

or something else that makes jQuery "see" the objects but not the user (and reset the used css-attributes in your myFrame.onload function).

like image 39
oezi Avatar answered Sep 18 '22 12:09

oezi