[英]Adding an event listener to an iframe
Is it possible to add an event listener to an iframe? 是否可以向iframe添加事件监听器? I've tried this code, but it doesn't seem to work:
我已经尝试过这段代码,但它似乎不起作用:
document.getElementsByTagName('iframe')[0].contentWindow.window.document.body.addEventListener('afterLayout', function(){
console.log('works');
});
I've also just tried using getting the element by id and adding my listener via the JavaScript framework I'm using, like this: 我也尝试过使用id获取元素并通过我正在使用的JavaScript框架添加我的监听器,如下所示:
Ext.fly("iframeID").addListener('afterLayout', function(){ alert('test'); });
I can call functions in the iframe, so I don't think security is an issue. 我可以在iframe中调用函数,所以我认为安全性不是问题。 Any ideas?
有任何想法吗?
I never tried to handle 'afterLayout' event but for more browser compatible code you'll use ( iframe.contentWindow || iframe.contentDocument
) instead of iframe.contentWindow
. 我从未试图处理'afterLayout'事件,但是对于更多与浏览器兼容的代码,您将使用(
iframe.contentWindow || iframe.contentDocument
)而不是iframe.contentWindow
。
try something like 尝试类似的东西
var iframe = document.getElementsByTagName('iframe')[0],
iDoc = iframe.contentWindow // sometimes glamorous naming of variable
|| iframe.contentDocument; // makes your code working :)
if (iDoc.document) {
iDoc = iDoc.document;
iDoc.body.addEventListener('afterLayout', function(){
console.log('works');
});
};
Hope it'll help. 希望它会有所帮助。
If you are doing serious iframe work in Ext, you should look into the ManagedIFrame user extension: 如果你在Ext中做了严肃的iframe工作,你应该查看ManagedIFrame用户扩展:
http://www.extjs.com/forum/showthread.php?t=40961 http://www.extjs.com/forum/showthread.php?t=40961
It features built-in events and cross-frame messaging, as well as many other benefits. 它具有内置事件和跨框架消息传递,以及许多其他好处。
Reasons for failure could be:- 失败的原因可能是: -
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.