Javascript 访问属性“href”的权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7798990/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:38:20  来源:igfitidea点击:

Permission denied to access property 'href'

javascriptpermission-denied

提问by

I try to reload parent web page from iframe. Here is my code:

我尝试从 iframe 重新加载父网页。这是我的代码:

 <script>
$(document).ready(function() {
  window.parent.location.href = window.parent.location.href;      
});
 </script>

But it doesn′t work. Firebug says: Permission denied to access property 'href'

但它不起作用。Firebug 说:访问属性“href”的权限被拒绝

I′m on same domain so what′s the problem? I try to do it in Wordpress theme.

我在同一个域上,所以有什么问题?我尝试在 Wordpress 主题中做到这一点。

回答by Krish_Arjun

Your code will be working only when the parent and child are running from the same protocol(http/https), same host and same port. This is known as Same Origin Policy (SOP). You can check the below reference: http://en.wikipedia.org/wiki/Same_origin_policy

只有当父子进程从相同的协议(http/https)、相同的主机和相同的端口运行时,您的代码才会工作。这称为同源策略 (SOP)。您可以查看以下参考:http: //en.wikipedia.org/wiki/Same_origin_policy

回答by lyl

You can set document.domain="domainName:port"in Firefox. It can't set a default value of port, you should set by yourself, if the port is different!

您可以document.domain="domainName:port"在 Firefox 中设置。不能设置端口的默认值,如果端口不同,请自行设置!