错误:无法访问属性href [英] Error: Can't access property href
问题描述
我使用NODE JS模块创建HTTP服务器。服务器的响应是一个包含嵌入网页的JavaScript的页面。
以下是响应码:
I am using NODE JS module with which I am creating a HTTP server. Server's response is a page containing JavaScript which embed a webpage in . Here is response code:
<html>
<head>
<script type='text/javascript'>
function test() {
document.body.innerHTML='<iframe id="ifool" src="URL" sandbox="allow-same-origin allow-forms allow-scripts"> </iframe>';
var c;
window.setInterval(function(){
c=document.getElementById("ifool").contentWindow.location.href;
window.history.pushState(0,0,c);
},100);
</script>
</head>
<body onload= "test()">
</body>
</html>
我正在使用Firebug与FF.I出现以下错误:
I am using Firebug with FF.I am getting following error:
Error: Permission denied to access property 'href'
c=document.getElementById("ifool").contentWindow.location.href;
推荐答案
如果您尝试在iframe中访问的网址超出了你当前的页面域,那么你将无法做到这一点。现代浏览器实现了同源策略,它在运行跨站点脚本时决定了JavaScript的权限。
If the URL you are trying to access in your iframe is outside of your current page domain then you will not be able to do it. Modern browsers implement a Same Origin Policy which decides the permissions of JavaScripts when running cross site scripting.
当父母和子女来自同一个域时,他们可以互相访问;孩子可以访问和操作父母的属性和方法,反之亦然。但是,如果不这样做,则尝试访问此类操作将触发脚本错误,指示权限被拒绝。
When a parent and child come from the same domain, they have access to each other; the child can access and operate properties and methods of the parent, and vice-versa. However, when they don't, attempting such access so will trigger script errors indicating Permission denied.
如果我尝试运行类似的脚本,并且页面和iframe源代码指向节点服务器托管的页面,则不会收到许可错误。
If I try running similar scripts with my page and iframe source pointing to pages hosted by node server, I get no permission errors.
来源:
Sources:
- http://www.esqsoft.com/javascript_examples/iframe_talks_to_parent/
- 错误:权限被拒绝访问属性'document' 从iframe访问父级网址
- <iframe> JavaScript跨域访问父DOM?
- http://www.esqsoft.com/javascript_examples/iframe_talks_to_parent/
- error : Permission denied to access property 'document'
- Access parent URL from iframe
- <iframe> javascript access parent DOM across domains?
这篇关于错误:无法访问属性href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!