“错误:ReferenceError:安全错误:试图读取受保护的变量:写入”与 Opera 11.5 和 window.open 和 document.write

发布于 2024-12-06 18:10:05 字数 825 浏览 7 评论 0原文

我遇到了 window.open 问题,该问题仅在 Opera(版本 11.5)中出现。

function openpageopera()
{
  var w = window.open("","_blank");
  try
  {
    w.document.write("<p>hello</p>world");
    w.document.close();
  }
  catch(err)
  {
    alert("error:" + err);
  }
}

现在,当我在页面加载后执行此函数时,它可以完美地工作,因此:

$(document).ready(function() {
  openpageopera();
});

如果我打开一个像 google、yahoo 这样的 URL,甚至从我的网络服务器打开一个网页,它也可以工作。

但是,当我将它附加到 onclick 事件(例如 HTML 按钮)时,它会失败。

<input type="button" value="Fails" onclick="openpageopera();">

具体是什么失败了?好吧,页面确实打开了,但它是空白的。 document.write 不起作用。该捕获指出了一个错误:

错误:ReferenceError:安全错误:尝试读取受保护的变量:写入

我不明白为什么在 html 按钮上执行此操作会引发安全错误。为什么会发生这种情况以及如何解决?

非常感谢您的帮助!

I am having an issue with window.open that only occurs with Opera (version 11.5).

function openpageopera()
{
  var w = window.open("","_blank");
  try
  {
    w.document.write("<p>hello</p>world");
    w.document.close();
  }
  catch(err)
  {
    alert("error:" + err);
  }
}

Now, it works flawlessly when I execute this function after the page has loaded, ergo:

$(document).ready(function() {
  openpageopera();
});

And it also work if I'm opening a URL like google, yahoo, or even a webpage from my web server.

However, when I attach it to an onclick event, like on an HTML button, it fails.

<input type="button" value="Fails" onclick="openpageopera();">

What fails specifically? Well, the page does open but it's blank. document.write is NOT working. The catch noted an error of:

error:ReferenceError: Security error: attempted to read protected variable: write

I don't understand why executing this on the html button raises a security error. Why is this occurring and how can I resolve it?

Thank you very much for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅语花开 2024-12-13 18:10:05

如果某处有代码设置document.domain,它可能会更改父页面的安全上下文。 (如果 document.domain 设置为其当前值,也会发生这种情况。)当您打开 "" 或 about:blank 时,它应该继承父级的安全上下文和 document.write() 应该可以工作 - 但我知道 Opera 过去有一些错误,其中设置 document.domain 在处理空弹出窗口时会导致问题。我相信这些错误已在 11.50 中修复,但似乎您遇到了类似的问题。如果可以的话,请避免设置 document.domain。

如果您看到此问题再次发生,请告诉我,最好让我查看完整的代码。我很想在每个版本中都解决这个错误。

顺便说一句,请记住,用户 JS、扩展,甚至由插件触发的 JS 也可能会扰乱 document.domain,因此请尝试禁用任何可能干扰的内容。

If there is code somewhere setting document.domain it might change the parent page's security context. (This also happens if document.domain is set to its current value.) When you open "" or about:blank it should inherit the parent's security context and document.write() should work - but I know Opera had some bugs in the past where setting document.domain would cause problems when working with empty popups. I believe those bugs are fixed in 11.50, but it seems as if you ran into a similar issue. Do avoid setting document.domain if you can..

If you see this problem occur again, please let me know and preferably let me see the full code. I'd love to nail this bug in every incarnation..

BTW, keep in mind that User JS, extensions, and even JS triggered from plugins might also mess around with document.domain, so try to disable anything that might interfere.

蹲在坟头点根烟 2024-12-13 18:10:05

我清除了 Opera 浏览器中的所有私人数据,结果成功了。然而,每次加载页面时,我都会确保按shift或ctrl刷新来清除缓存。要么是缓存问题,要么完全是其他问题。

I ended clearing all private data in the Opera browser and it worked. However, every time I loaded the page, I made sure to press shift or ctrl refresh to clear the cache. Either it was a cache issue or something else entirely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文