views:

576

answers:

5

Is there any way to stop an Iframe re-loading it's contents when I change it's 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.

+1  A: 

A quick guess would be to unset the value of the src attribute of the iframe element or set it to "about:blank".

It is up to you to restore the previous value (or any value) to the src attribute of the iframe (using JavaScript).

Regards,

andreas
It sounds to me like the OP is wanting it to keep the same contents and not refresh them.
Renesis
Renesis yeah, that's right :)andreas - unsetting the src attribute and resetting it again would still reload the page!
James Crowley
thanks a lot for the down-vote...
andreas
sorry - I don't use SO that often. no idea if down voting is appropriate then or not. really appreciate your help but it didn't solve the problem :) I have removed the downvote.
James Crowley
No need to down-vote answers, unless they are totally, obviously wrong or misguided. I've given you +1, andreas. Thanks for your efforts mate.
roosteronacid
+3  A: 

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.
Renesis
Essentially, 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
James Crowley
the alternative I considered was moving the loading element so it takes on the position and location of the target element (instead of adopting it). however, that seemed to open up a whole world of pain in making sure it stayed in the right place? (as if the page reflowed, the target element might move - but the absolutely positioned one would stay in the wrong place).if you think this route might be worth exploring, I will post an alternative question?thanks very much for your help
James Crowley
thanks renesis. unfortunately iframes then create fun with cross domain issues (for the ad serving tags that actually need direct access). yippee.for #3 I hit some problems when I explored this - I've just posted a new question at http://stackoverflow.com/questions/2233337/how-to-keep-an-absolutely-positioned-element-directly-over-the-position-of-inline ... would appreciate any thoughts you might have?
James Crowley
Well if you have a dynamic site that can change appearance and reflow, then yeah, you've got issues. :) So now, I am wondering what exactly is the problem that motivated you to load this content at the end of the page?
Renesis
Performance :) Loading ads is *slow*.But... think we're gonna go with the absolute positioning solution, and just accept that it won't work for certain publishers without more effort on their part.Thanks for your help!
James Crowley
A: 

What about using an ajax request to load the ad's contents, and adding the contents to the DOM when the ajax call returns, instead of using an iframe?

RMorrisey
unfortunately standard ad serving tags don't lend themselves well to this - they all depend on using document.write, and generally have multiple <script> tags. ajax-ing in that kind of content would just break once I place it in the DOM, because the document.writes would just be appearing elsewhere.
James Crowley
+1  A: 

If the ads are a fixed size, you could place them in absolutely-positioned divs instead. Then, once the page loads, you could move those container divs to their designated spots. There are a lot of Javascript samples out there for calculating an absolute position from a relative position. Of course, you would have to reserve space visually in the destination divs so the ads wouldn't cover the content.

Jacob
Hi Jacob - thanks for that. It's looking like this is the best approach. I have tried it before but hit a few issues with it - check out my post here: http://stackoverflow.com/questions/2233337/how-to-keep-an-absolutely-positioned-element-directly-over-the-position-of-inline - would appreciate any input you might have
James Crowley
+1  A: 

I'm regretting my original answer, as it seems to be causing other headaches. Here's a few other potential solutions that you may not have tried:

  • Place the ad scripts inside of divs with their display style set to none. Then, move them to their final desintation and change them to display: block after the page has loaded. Perhaps this would prevent the iframes from loading their content inititially.
  • Try the same thing, only with visibility set to hidden, then changed to visible.
Jacob
Nice... another approach would be to load them as pure text using a micro-templating approach: `<script id="ad1" type="text/html"><iframe src="whatever"/></script>` then `$('#placeholder').html($('#ad1').html())` will load that text into place (and cause it to be rendered at that time). See http://ejohn.org/blog/javascript-micro-templating/ from the creator of jQuery himself.
Renesis
'tis a sweet idea, thanks jacob. unfortunately doesn't seem to stop either browser loading in the frame twice!
James Crowley
renesis - unfortunately because the iframe would be written out by the ad server, as part of a document.write, I'm not sure I could get it to appear within a script tag? interesting though. will take a look.
James Crowley