【发布时间】:2013-01-20 20:18:06
【问题描述】:
当document.domain 更改时,是否有人知道在 IE 中的页面上创建 about:blank iframe 的任何解决方法?
在document.domain 属性更改后,IE 似乎不允许访问空/动态 iframe。
例如,假设您正在动态创建一个 iframe,然后将一些 html 注入其中:
// Somewhere else, some 3rd party code changes the domain
// from something.foo.com to foo.com
document.domain = 'jshell.net';
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
// In IE, we can't access the iframe's contentWindow! Access is denied.
iframe.contentWindow.document.body.style.backgroundColor = 'red';
这是一个关于 jsfiddle 的实时示例:http://jsfiddle.net/XHkUT/
您会注意到它在 FF/Webkit 中运行良好,但在 IE 中却不行。这尤其令人沮丧,因为这会影响在 document.domain 属性更改后创建的 iframe(如上例所示)。
IE 规则似乎是“如果在更改 document.domain 后创建动态/空 iframe,则无法访问其 DOM。”
将 iframe src 设置为 about:blank javascript:void(0) 或 javascript:"" 失败。
【问题讨论】:
-
about: 协议可能会受到限制,具体取决于 IE 设置/版本:msdn.microsoft.com/en-us/library/ee330729(v=vs.85).aspx(请参阅“关于协议限制”一章)
-
注意:这个错误似乎已在 IE11 中修复。
标签: javascript internet-explorer dom iframe cross-domain-policy