有谁知道创建一个about:blank
IE 中页面上的 iframe 时document.domain
已经改变?
IE 似乎不允许在之后访问空/动态 iframedocument.domain
财产已被改变。
例如,假设您正在动态创建一个 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/ http://jsfiddle.net/XHkUT/
您会注意到它在 FF/Webkit 中运行良好,但在 IE 中则不然。这特别令人沮丧,因为这会影响创建的 iframeafter the document.domain
属性已更改(如上例所示)。
IE 规则似乎是“如果您在更改后创建动态/空 iframedocument.domain
,你无法访问它的 DOM。”
设置 iframesrc
to about:blank
javascript:void(0)
or javascript:""
已经不成功了。
您愿意将 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>');
编辑:如果这是页面上的内联脚本,那么您需要拆分结束脚本</script>
标记起来。看为什么分割脚本标签 https://stackoverflow.com/questions/236073/why-split-the-script-tag-when-writing-it-with-document-write
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)