Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop an iframe reloading when I change its position in the DOM?

Is there any way to stop an iframe re-loading its contents when I change its position within the DOM? Simple example:

<script type="text/javascript">
function moveiframe() {
    var dest = document.getElementById('newparent');
    dest.appendChild(document.getElementById('googleframe'));
}
</script>
<iframe src="http://www.google.com" id="googleframe"></iframe>
<input type="button" onclick="moveiframe()" value="Move" />

clicking the "Move" button changes the parent of the iframe, and reloads its contents (in Firefox and Chrome, but not IE).

Any suggestions would be much appreciated!

[Updated with background info]

I'm loading the site's adverts in placeholder divs at the bottom of the page (to prevent advert loading from holding up the page load) - and then shifting the divs they've been written in to their correct container once loaded. It all works great... unless the ad that gets served uses an iframe (like google adsense) in which case the ad gets loaded twice and the serving is messed up.

like image 270
James Crowley Avatar asked Feb 09 '10 22:02

James Crowley


People also ask

How do I stop redirecting iframe?

You can set sandbox="" , which prevents the iframe from redirecting. That being said it won't redirect the iframe either.

Can an iframe show only part of page?

Set the iframe to the appropriate width and height and set the scrolling attribute to "no". If the area you want is not in the top-left portion of the page, you can scroll the content to the appropriate area. Refer to this question: Scrolling an iframe with javascript?

Does iframe work if javascript is disabled?

If a user has javascript disabled, iframes will work. An iframe tag has attributes “height” and “width,” which allows the designer great latitude with dimensions and format like 300×250 , 728×90 depending on the Ad size. Iframe tag can appear anywhere on the page and several iframes can be added if wished to.

Can you reload an iframe?

reload() will reload the iframe. This works from JavaScript inside the current iFrame.


Video Answer


1 Answers

Considering the simplicity of your test case, it looks like the only methods you have available to put an element inside another will always force the contents to reload.

[Edit] After seeing what you're trying to do, there are a couple things you can try:

  1. All ads could be served in IFRAMEs (on your own site) which will not hold up loading of the page and can be placed in the right place right away.
  2. As mentioned above IFRAMEs won't hold up loading of the page so you could put the ads in place right away if they are IFRAMEs and load them at the bottom if they are something else. Of course, this won't work if the slow part is before you know if you are going to get an IFRAME or not.
  3. You could keep the content in it's placeholder DIV but when it's done loading just reposition (with CSS absolute positioning) over the right place in the page. Not the most elegant solution, but should work fine.
like image 141
Nicole Avatar answered Sep 24 '22 19:09

Nicole