在document.domain更改后,IE中无法访问about:空白iframe [英] Can't access an about:blank iframe in IE after the document.domain changes

查看:212
本文介绍了在document.domain更改后,IE中无法访问about:空白iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道在$ code> document.domain 的任何解决方法:空白 iframe在IE的页面上c>已经改变了?

Does anyone know of any workarounds to creating an about:blank iframe on a page in IE when the document.domain has changed?

document.domain 属性已被更改后,IE似乎不允许访问空/动态iframe 。

IE doesn't seem to allow access to empty/dynamic iframes after the document.domain property has been altered.

例如,假设您正在动态创建一个iframe,然后在其中注入一些html:

For example, imagine you're dynamically creating an iframe and then injecting some html into it:

// 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/

Here's a live example on jsfiddle: http://jsfiddle.net/XHkUT/

您会注意到它在FF / Webkit中正常工作,但不是IE。这是特别令人沮丧的,因为这影响了

You'll notice it works fine in FF/Webkit, but not IE. It's particularly frustrating because this affects iframes created after the document.domain property has changed (as in the example above).

IE规则似乎是如果您在更改 document.domain 之后创建一个动态/空iframe,您不能访问其DOM。

The IE rule seems to be "if you create a dynamic/empty iframe after you change document.domain, you can't access its DOM."

将iframe src 设置为:空白 javascript:void(0) javascript:已成功/ p>

Setting the iframe src to about:blank javascript:void(0) or javascript:"" has been unsuccessful.

推荐答案

您是否乐意将iframe的域名更改为?以下作品(对我来说)在IE7,9中

Are you happy to change the domain of the iframe to? The following works (for me) in 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>');

编辑:如果这是页面上的内联脚本,则需要拆分结束< / script> 标记。请参阅为什么分拆脚本标记

If this is inline script on a page then you need to split the closing </script> tag up. See why-split-the-script-tag

这篇关于在document.domain更改后,IE中无法访问about:空白iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆