在document.domain更改后,无法在IE中访问about:blank iframe

smi*_*lay 10 javascript iframe internet-explorer dom cross-domain-policy

有没有人知道about:blank在IE document.domain更改时在IE页面上创建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';
Run Code Online (Sandbox Code Playgroud)

这是jsfiddle的实例:http://jsfiddle.net/XHkUT/

您会注意到它在FF/Webkit中运行良好,但不适用于IE.这特别令人沮丧,因为这会影响属性更改创建的iframe document.domain(如上例所示).

IE规则似乎是"如果您在更改后创建动态/空iframe document.domain,则无法访问其DOM."

将iframe设置srcabout:blank javascript:void(0)或未javascript:""成功.

Sea*_*gan 5

您是否乐意将iframe的域名更改为?IE7,9中的以下工作(对我而言)

document.domain = 'jshell.net';

var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.src = "javascript:document.write('<script>document.domain=\"jshell.net\"</script>')";

// Now write some content to the iframe
iframe.contentWindow.document.write('<html><body><p>Hello world</p></body></html>');
Run Code Online (Sandbox Code Playgroud)

编辑:如果这是页面上的内联脚本,那么您需要拆分结束</script>标记.请参阅why-split-the-script-tag