当我更改iframe在DOM中的位置时,如何停止iframe的重新加载? [英] How can I stop an iframe reloading when I change its position in the DOM?

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

问题描述

当我更改DOM在DOM中的位置时,是否有任何方法可以阻止iframe重新加载其内容?简单的例子:

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" />

点击移动"按钮更改iframe的父项,然后重新加载其内容(在Firefox和Chrome中,而不是IE).

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

任何建议将不胜感激!

[已更新背景信息]

[Updated with background info]

我正在页面底部的占位符div中加载网站的广告(以防止广告加载阻止页面加载)-然后在加载后将已写入的div转移到其正确的容器中.一切都很好...除非投放的广告使用iframe(例如google adsense),否则这种情况下广告会加载两次,并且投放变得混乱.

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. 所有广告都可以在IFRAME(在您自己的网站上)中投放,这不会阻止页面的加载,而是可以立即放置在正确的位置.
  2. 如上所述,IFRAME不会阻止页面加载,因此如果它们是IFRAME,则可以立即将广告放到适当的位置,如果其他条件则可以将它们加载到底部.当然,如果最慢的部分在您不知道是否要获得IFRAME之前就行不通了.
  3. 您可以将内容保留在占位符DIV中,但是在完成加载后,只需在页面的正确位置上重新定位(使用CSS绝对定位)即可.不是最优雅的解决方案,但应该可以正常工作.

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

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