【问题标题】:How to set document.domain for a dynamically generated IFRAME?如何为动态生成的 IFRAME 设置 document.domain?
【发布时间】:2011-01-30 00:33:23
【问题描述】:

我在需要声明 document.domain 的页面上实现 CodeMirror (http://marijn.haverbeke.nl/codemirror/)(因为页面上有其他 IFRAMES)。

CodeMirror 生成动态 IFRAME 以提供语法高亮代码编辑。问题是IE在下面的一段代码镜像代码中抛出了'Access Denied'(其他浏览器都可以):

this.win = frame.contentWindow;
...
var doc = this.win.document; <-- ERROR
doc.open();
doc.write(html.join(""));
doc.close();

事实证明 IE 没有从父 IE 继承 document.domain。我可以在 IFRAME 内容中设置 document.domain,但 IE 在我设置内容之前就抛出了错误。任何想法如何解决这个问题?

【问题讨论】:

标签: javascript iframe internet-explorer access-denied


【解决方案1】:

终于搞定了。受 TinyMCE 代码启发的 hack。

var u = 'javascript:(function(){document.open();document.domain="' + document.domain + '";var ed = window.parent.CodeMirror_boilerplate;document.write(ed);document.close();})()';

frame.src = u;

它将 document.domain 设置在 SRC 中,而不是通过 DOM。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多