2014-09-04 115 views
0

我有一個頁面,其中包含一個從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錯誤

我無法使用絕對路徑的原因。父窗口鏈接將以某種方式改變。我認爲最好的解決方案是採取父網址,並追加我想要的參數。我如何在不發生安全錯誤的情況下做到這一點?

回答

1

簡答題是「你不行」。

瀏覽器安全模型排除了客戶端的跨域和跨協議腳本。您的https協議下的嵌入式iframe不允許訪問(甚至不讀取)其父節點的非https上下文。

要做你想做的事情,爲了在客戶端進行交互,兩個上下文必須同時在原始域和協議上達成一致。