问题描述
当我尝试从包含另一个域 url 的 iframe 为父 url 设置哈希值时,出现以下错误:
I am getting the below error when i try to set a hash value to the parent url from iframe which contains another domain url:
不安全的 JavaScript 尝试从 URLURL2"的框架访问 URLURL1"的框架.域、协议和端口必须匹配.
我该如何解决这个问题?
How can I fix this problem?
推荐答案
不允许从不同来源的子文档访问顶部窗口的 location.hash
属性,但可以访问设置 location
属性本身.
From a child document of different origin you are not allowed access to the top window's location.hash
property, but you are allowed to set the location
property itself.
这意味着假设顶部窗口位置是http://example.com/page/
,而不是做
This means that given that the top windows location is http://example.com/page/
, instead of doing
parent.location.hash = "#foobar";
你确实需要知道父母的位置并且做
you do need to know the parents location and do
parent.location = "http://example.com/page/#foobar";
由于资源未导航,这将按预期工作,仅更改 url 的哈希部分.
Since the resource is not navigated this will work as expected, only changing the hash part of the url.
如果您将其用于跨域通信,那么我建议您改用 easyXDM.
If you are using this for cross-domain communication, then I would recommend using easyXDM instead.
这篇关于不安全的 JavaScript 尝试使用 URL 访问框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!