【问题标题】:javascript iframe: accessing http from httpsjavascript iframe:从 https 访问 http
【发布时间】:2014-09-05 02:14:50
【问题描述】:

我有一个页面,它有一个 iframe,它从亚马逊 aws s3 存储桶加载 html 模板。在iframe中,我想取父窗口的链接,然后添加一些参数。

例如:我的父窗口有链接 http://xx.xx.x.xxx:8088 。我想获取此网址,附加“#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 中重定向到另一个页面的 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 并附加我想要的参数。如何在不出现安全错误的情况下实现这一点?

【问题讨论】:

    标签: javascript html iframe amazon-s3


    【解决方案1】:

    简短的回答是“你不能”。

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

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

    【讨论】:

      猜你喜欢
      • 2013-08-30
      • 2011-09-25
      • 2012-08-01
      • 2011-03-09
      • 2010-10-30
      • 1970-01-01
      • 2020-03-08
      • 2018-01-06
      • 1970-01-01
      相关资源
      最近更新 更多