JS报错:Permission denied to access property 'document'---document.domain - JavaScript的同源策略问题
2014-05-07 10:04
337 查看
错误原因是因为父窗口与子窗口的域名不同,即便同一站点下,也需要写全引用地址:如同是xxx.com下的页面<iframe id="iframe_xxxx" name="iframe_xxxx" src ="/sub.html" frameborder="0" height="300" width="1000"></iframe>与<iframe id="iframe_xxxx" name="iframe_xxxx"
src ="http://www.xxx.com/sub.html" frameborder="0" height="300" width="1000"></iframe>是有区别的
笔者在做第三方集成时,使用了<iframe>,通过子页面的js来调整父页面的元素时,会出现访问不到的情况。
错误信息:Permission denied to access property 'document'
造成这个问题的原因是js不属于同一个域,由于某些浏览器的安全问题,所以被禁止访问了。
参考资料:https://developer.mozilla.org/Cn/JavaScript的同源策略(如不能访问,改https为http)
文件:http://sub.xxx.com/index.html
<!doctype html>
<html>
<head>
...
</head>
<body>
...
<iframe id="iframe_xxxx" name="iframe_xxxx" src ="http://www.xxx.com/sub.html" frameborder="0" height="300" width="1000"></iframe>
</body>
</html>
文件:http://www.xxx.com/sub.html
<!doctype html>
<html>
<head>
<script type="text/javascript">
//设置域信息
document.domain = 'xxx.com';
//设置父级页面引用自身的iframe高度
function setHeight(){
//判断是否为顶级页面
if(window.top!=window.self){
parent.document.getElementById('iframe_xxxx').height=document.body.scrollHeight+20
}
}
</script>
</head>
<body onload="setHeight();">
...
</body>
</html>
============================================================
这个问题是浏览器的安全机制造成的,
但是这种情况是同一域名下的一级子域名和二级子域名的区别,还是可以解决的。
解决方法就是把两个页面的域信息进行修改,变为相同的即可。
因为默认页面的域信息是包含二级域名的,这样设置可以统一使用顶级域名作为域信息。
<script type="text/javascript">
document.domain = 'xxx.com';
</script>
注:如果两个页面不属于同一个域名下,此方法不可行。强行设置js会报错。
补充:如果我们不确定引用的页面是否和被引用的页面所在域相同,可通过获取引用页面的url地址来判断
<script type="text/javascript">
//document.referrer - 可返回载入当前文档的文档的 URL。
$p_url = document.referrer;
</script>
//============================================================
参考:http://hi.baidu.com/eecc00/item/7c99477420e9f1345c1789e3
src ="http://www.xxx.com/sub.html" frameborder="0" height="300" width="1000"></iframe>是有区别的
笔者在做第三方集成时,使用了<iframe>,通过子页面的js来调整父页面的元素时,会出现访问不到的情况。
错误信息:Permission denied to access property 'document'
造成这个问题的原因是js不属于同一个域,由于某些浏览器的安全问题,所以被禁止访问了。
参考资料:https://developer.mozilla.org/Cn/JavaScript的同源策略(如不能访问,改https为http)
文件:http://sub.xxx.com/index.html
<!doctype html>
<html>
<head>
...
</head>
<body>
...
<iframe id="iframe_xxxx" name="iframe_xxxx" src ="http://www.xxx.com/sub.html" frameborder="0" height="300" width="1000"></iframe>
</body>
</html>
文件:http://www.xxx.com/sub.html
<!doctype html>
<html>
<head>
<script type="text/javascript">
//设置域信息
document.domain = 'xxx.com';
//设置父级页面引用自身的iframe高度
function setHeight(){
//判断是否为顶级页面
if(window.top!=window.self){
parent.document.getElementById('iframe_xxxx').height=document.body.scrollHeight+20
}
}
</script>
</head>
<body onload="setHeight();">
...
</body>
</html>
============================================================
这个问题是浏览器的安全机制造成的,
但是这种情况是同一域名下的一级子域名和二级子域名的区别,还是可以解决的。
解决方法就是把两个页面的域信息进行修改,变为相同的即可。
因为默认页面的域信息是包含二级域名的,这样设置可以统一使用顶级域名作为域信息。
<script type="text/javascript">
document.domain = 'xxx.com';
</script>
注:如果两个页面不属于同一个域名下,此方法不可行。强行设置js会报错。
补充:如果我们不确定引用的页面是否和被引用的页面所在域相同,可通过获取引用页面的url地址来判断
<script type="text/javascript">
//document.referrer - 可返回载入当前文档的文档的 URL。
$p_url = document.referrer;
</script>
//============================================================
参考:http://hi.baidu.com/eecc00/item/7c99477420e9f1345c1789e3
相关文章推荐
- JS报错:Permission denied to access property 'document'---document.domain - JavaScript的同源策略问题
- JS报错:Permission denied to access property 'document'---document.domain - JavaScript的同源策略问题
- JS报错:Permission denied to access property 'document'---document.domain - JavaScript的同源策略问题
- document.domain - JavaScript的同源策略问题:错误信息:Permission denied to access property 'document'_eecc00_百度空间
- js 遇到 Permission denied to access property ***
- 解决FF中的bug:JavaScript focus() throws "Permission denied to get property XULElement.selectedIndex"
- Ext报错:Permission denied to access property 'dom' from a non-chrome context
- Permission denied to access property 'dom' from a non-chrome context
- Permission denied to access property 'dom' from a non-chrome context
- <context:property-placeholder>导致的 Access denied for user ''@'localhost' to database 问题
- 报错 Error: Permission denied to access property "href"
- 彻底地/ 终于地, 解决 关于apache 权限的问题了:: 修改 DocumentRoot后的 403错误: have no permission to access / on this server
- Permission denied to access property 'dom' from a non-chrome context
- Mysql - 解决Access denied for user ''@'localhost' to database 'mysql'问题
- Wamp 修改默认网站目录的方法及出现You don't have permission to access / on this server问题解决
- linux Mysql - 解决Access denied for user ''@'localhost' to database 'mysql'问题
- Apache配置问题Forbidden You don't have permission to access / on this server
- Mysql - 解决Access denied for user ''@'localhost' to database 'mysql'问题
- linux下的LAMP环境中的问题:failed to open stream: Permission denied in
- 在用VSS时遇到的一个问题:Access to file denied 或 Unable to open user login file