当 mootools 操作时 iframe 内容消失
我有一个第三方(mootools)库创建标签,我有谷歌双击发布商(dfp)创建广告。 dfp 在 iframe 中创建广告,然后选项卡脚本获取 iframe 的锚点并用它进行“混乱”以创建选项卡。 iframe 的内容会在此过程中丢失。
我正在寻找一种解决此问题的方法(尝试在选项卡加载后触发 dfp 内容,但随后 google 脚本崩溃了)。
iframe 与父窗口来自不同的域,因此尝试对 iframe 内的元素执行任何操作都将失败。
addTab: function(text, title, content) {
var grab = $(content);
var container = (grab || new Element('div'))
.setStyle('display', 'none')
.addClass(this.options.classContainer);
this.wrapper.adopt(container);
var pos = this.tabs.length;
var evt = (this.options.hover) ? 'mouseenter' : 'click';
var tab = {
container: container,
toggle: new Element('li').grab(new Element('a', {
href: '#',
title: title
}).grab(
new Element('span', {html: text})
)).addEvent(evt, this.onClick.bindWithEvent(this, [pos])).inject(this.menu)
};
if (!grab && $type(content) == 'string') tab.url = content;
this.tabs.push(tab);
return this.fireEvent('onAdded', [tab.toggle, tab.container, pos]);
},
I've got a third party (mootools) library creating tabs and I've got google double click for publishers (dfp) creating ads. dfp creates the ads in an iframe then the tabs script grabs an anchestor of the iframe and 'messes' with it to create the tabs. The contents of the iframe gets lost in the process.
I'm looking for a way of coping with this (tried firing the dfp stuff after the tabs had loaded but then the google scripts crashed).
The iframe is from a different domain to the parent window so anything which tries to do stuff to elements within the iframe is going to fail.
addTab: function(text, title, content) {
var grab = $(content);
var container = (grab || new Element('div'))
.setStyle('display', 'none')
.addClass(this.options.classContainer);
this.wrapper.adopt(container);
var pos = this.tabs.length;
var evt = (this.options.hover) ? 'mouseenter' : 'click';
var tab = {
container: container,
toggle: new Element('li').grab(new Element('a', {
href: '#',
title: title
}).grab(
new Element('span', {html: text})
)).addEvent(evt, this.onClick.bindWithEvent(this, [pos])).inject(this.menu)
};
if (!grab && $type(content) == 'string') tab.url = content;
this.tabs.push(tab);
return this.fireEvent('onAdded', [tab.toggle, tab.container, pos]);
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当 iframe 或其祖先与 DOM 断开连接时,iframe 的内容将被清除,并且当它或其祖先重新插入时,iframe 将重新加载。这种烦人的行为发生在 Firefox 和 Webkit 中,但我认为 iframe 不会在 IE 上重新加载。
作为解决方法,您需要找到一种方法来重新排序代码,以便仅在选项卡脚本完成对容器的操作后添加 iframe。
Chrome 显然还有一个选项,如果您使用 acquireNode 而不是 apendChild 移动 iframe,它会导致 iframe 不会重新加载,但我不相信它是跨浏览器的。
有关更多信息,请参阅类似的问题:如何在 DOM 中移动 iFrame 而不丢失其状态?
Whenever an iframe or an ancestor of it are disconnected from the DOM, the iframe's contents will be cleared and the iframe will reload when it or its ancestor is reinserted back. This anoying behaviour occurs in Firefox and Webkit, but I think the iframes won't reload on IE.
As a workaround, you will need to find a way to reorder your code so that the iframe is only added after the tab script is done doing its thing to the container.
Chrome also apparently has an option that causes the iframe to not reload if you move it with adoptNode instead of apendChild but I do not believe it is cross-browser.
for more, see this similar question: How to move an iFrame in the DOM without losing its state?