在文档域已更改时,是否有任何解决方法可以在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:""
都没有成功。