javascript iframe:从https访问http
问题描述:
我有一个页面,其中包含一个从amazon aws s3存储区加载html模板的iframe。在iframe中,我想获取父窗口的链接,然后添加一些参数。javascript iframe:从https访问http
例如:我的父窗口有链接http://xx.xx.x.xxx:8088。我想采取此网址,apprend“#abc/edf”并重定向到该页面。但我不能,因为我的iFrame拥有URL https://bucketName.s3.amazonaws.com
我得到的是
Uncaught SecurityError: Blocked a frame with origin "https://bucketName.s3.amazonaws.com" from accessing a frame with origin "http://xx.xx.x.xxx:8088". The frame requesting access has a protocol of "https", the frame being accessed has a protocol of "http". Protocols must match.
我用来重定向到另一个页面,从一个iFrame
function navigateTo(e){
var destination = e.data.destination;
var url = window.parent.location.host;
window.parent.location.href = url+destination;
}
$("#button").click({destination: "/#abc/edf"}, navigateTo);
HTML
<div id="button"><a href="#">Redirect to another page</a></div>
在JavaScript错误
我无法使用绝对路径的原因。父窗口链接将以某种方式改变。我认为最好的解决方案是采取父网址,并追加我想要的参数。我如何在不发生安全错误的情况下做到这一点?
答
简答题是“你不行”。
浏览器安全模型排除了客户端的跨域和跨协议脚本。您的https协议下的嵌入式iframe不允许访问(甚至不读取)其父节点的非https上下文。
要做你想做的事情,为了在客户端进行交互,两个上下文必须同时在原始域和协议上达成一致。