1

我正在将代码从使用签名的 JAR 文件转换为使用基于 XULRunner 的应用程序。我在加载带有存储在 javascript 变量中的 html 内容的 iframe 的代码时遇到问题。

代码如下所示:

var doc = iframe.contentDocument;
doc.open();
doc.write(html);
doc.close();

iframe 具有 type="content"。照原样,在 XULRunner 中,我在 doc.open() 调用中遇到异常:

[Exception... "The operation is insecure."
   code: "18"
   nsresult: "0x80530012 (SecurityError)"
   location: "chrome://ec4main/content/apps/newsfeedtest/lib.js Line: 938"]

如果我将 iframe 更改为 type="chrome",那么它可以工作,但这似乎是个坏主意,因为 HTML 并不总是受信任的内容。

4

1 回答 1

4

您应该使用数据 URL而不是document.write()(这确实不安全且不推荐):

var wnd = iframe.contentWindow;
wnd.location.href = "data:text/html;charset=utf-8," + encodeURIComponent(html);
于 2012-10-16T09:10:05.237 回答