javascript - IE8中访问iframe Object.prototype

标签 javascript iframe internet-explorer-8

我在页面上有一个 Iframe 元素,我需要为 iframe 内的对象创建一些原型(prototype)方法。

我有 iframe 窗口

var win = iframe.contentWindow || iframe.contentDocument.defaultView;

然后尝试创建方法。在 FF、Chrome 和 IE9+ 中,这很简单:

win.Object.prototype.func = myFunc;

但它在 IE8 中不起作用。我在 win.Object 中未定义。 同时win.Element工作完美。

如何在 IE8 中获取 iframe 对象?

最佳答案

您可以在 iframe 中插入一个 script 元素,该元素在外部窗口中创建一个指向内部对象的全局变量:

var doc = iframe.contentDocument,
    s = doc.createElement('script');
s.text = "parent.Object2 = Object;";
doc.appendChild(s);
doc.removeChild(s);

然后,您可以使用Object2.prototype

关于javascript - IE8中访问iframe Object.prototype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31547091/

相关文章:

javascript - 获取具有特定名称的cookie

javascript - 检测 iframe 何时跨域,然后清除它

internet-explorer - IE9 的 IE8 仿真和老实说的 IE8 有什么区别?

当开发者工具关闭时,Javascript 在 IE 中失败

javascript - meteor :未捕获类型错误:无法读取未定义的属性 'events'

javascript - AngularJS:将服务属性绑定(bind)到 Controller

javascript - 允许用户上传 HTML/JS 文件的风险

javascript - Firefox element.event 有效,但 element.addEventlistener 无效

javascript - jQuery noConflict() 问题

javascript - 如何在单击按钮时加载部分 View ?