Javascript 如何为动态生成的 IFRAME 设置 document.domain?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2486901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set document.domain for a dynamically generated IFRAME?
提问by Paras Chopra
I am implementing CodeMirror (http://marijn.haverbeke.nl/codemirror/) on a page where document.domain needs to be declared (because of other IFRAMES on the page).
我在需要声明 document.domain 的页面上实现 CodeMirror ( http://marijn.haverbeke.nl/codemirror/)(因为页面上有其他 IFRAMES)。
CodeMirror generates a dynamic IFRAME to provide syntax highlighted code editing. The problem is that IE throws up 'Access Denied' (other browsers are fine) at the following piece of code mirror code:
CodeMirror 生成动态 IFRAME 以提供语法突出显示的代码编辑。问题是IE在以下代码镜像代码中抛出“拒绝访问”(其他浏览器没问题):
this.win = frame.contentWindow;
...
var doc = this.win.document; <-- ERROR
doc.open();
doc.write(html.join(""));
doc.close();
It turns out IE doesn't inherit document.domain from parent IE. I can set document.domain in the IFRAME contents but IE throws up the error before I can even set the contents. Any ideas how to tackle this problem?
事实证明 IE 没有从父 IE 继承 document.domain。我可以在 IFRAME 内容中设置 document.domain 但 IE 在我什至可以设置内容之前抛出错误。任何想法如何解决这个问题?
回答by Paras Chopra
Got it to work, finally. A hack inspired by TinyMCE code.
得到它的工作,终于。受 TinyMCE 代码启发的黑客攻击。
var u = 'javascript:(function(){document.open();document.domain="' + document.domain + '";var ed = window.parent.CodeMirror_boilerplate;document.write(ed);document.close();})()';
frame.src = u;
frame.src = u;
It sets the document.domain in SRC and not by DOM.
它在 SRC 而不是 DOM 中设置 document.domain。

