开发者

"error:ReferenceError: Security error: attempted to read protected variable: write" with Opera 11.5 and window.open and document.write

开发者 https://www.devze.com 2023-04-08 04:33 出处:网络
I am having an issue with window.open that only occurs with Opera (version 11.5). function openpageopera()

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. docume开发者_开发知识库nt.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!


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号