javascript iframe: accessing http from https

javascript iframe: accessing http from https

本文关键字:from https http accessing iframe javascript      更新时间:2023-09-26

我有一个页面,它有一个iframe,可以从amazon aws s3 bucket加载html模板。在iframe中,我想获取父窗口的链接,然后添加一些参数。

E.g:我的父窗口有链接http://xx.xx.x.xxx:8088。我想获取这个url,append"#abc/edf"并重定向到那个页面。但我不能,因为我的iFrame有urlhttps://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 重定向到另一个页面的javascript

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>

我不能使用绝对路径是有原因的。父窗口链接将以某种方式更改。我认为最好的解决方案是获取父url并附加我想要的参数。如何在不出现安全错误的情况下实现此操作?

简短的回答是"你不能。"

浏览器安全模型排除了客户端的跨域和跨协议脚本。https协议下嵌入的iframe不允许访问(甚至不允许读取)其父级的非https上下文。

要执行您想要的操作,两个上下文必须在源域和协议上达成一致,以便在客户端进行交互。