当我改变它在DOM中的位置时,如何停止IFrame重新加载? [英] How can I stop an IFrame reloading when I change it's position in the DOM?

查看:255
本文介绍了当我改变它在DOM中的位置时,如何停止IFrame重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法阻止Iframe在DOM内改变位置时重新加载内容?简单的例子:

 < script type =text / javascript> 
function moveiframe(){
var dest = document.getElementById('newparent');
dest.appendChild(document.getElementById('googleframe'));
}
< / script>
< iframe src =http://www.google.comid =googleframe>< / iframe>
< input type =buttononclick =moveiframe()value =Move/>点击移动按钮更改iframe的父级,然后重新加载其内容(在Firefox中),然后单击添加 Chrome浏览器,但不IE浏览器)。

任何建议将不胜感激!
$ b [更新与背景信息]



我在页面底部的站点分区中加载网站的广告(以防止广告加载阻止页面加载) - 然后将div一旦装入,就被写入正确的容器。这一切都很好,除非获得的广告使用iframe(如谷歌的AdSense),在这种情况下,广告获得了两次加载和服务搞砸了。

解决方案

考虑到你的测试用例的简单性,看起来你可以将元素放在另一个元素中的唯一方法总是强制重载内容。



[编辑]在看到您要做的事情之后,您可以尝试以下几项:


  1. 所有广告都可以在IFRAME中投放(在您自己的网站上),这样不会占用页面的空间,并且可以立即放置在正确的位置。
  2. 如上所述,IFRAME不会阻止加载页面,因此如果他们是IFRAME,可以立即将广告放在适当位置,如果他们是其他内容,则将其加载到底部。当然,如果缓慢的部分是在你知道你是否要获得一个IFRAME之前,这是行不通的。

  3. 你可以将内容保留在占位符DIV中,但是当它完成加载只是重新定位(与CSS绝对定位)在页面的正确位置。不是最优雅的解决方案,但应该正常工作。


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.

解决方案

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.

这篇关于当我改变它在DOM中的位置时,如何停止IFrame重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆