2016-11-16 126 views
0

所以我想實現的是一個interent iframe。此iframe將與當前父網址(http://parenturl.com)重合。交互式iframe

所以說我有一個iframehttp://www.google.com,我點擊一個鏈接在說iframe。

這將父網址更新爲http://parenturl.com#www.google.com/link2iframe然後轉到您單擊的位置。

我也想要父網址接受參數,所以如果我這樣做http://parenturl.com#bing.com它會去http://bing.com

的iframe代碼:iframe中,每一次點擊

<iframe src="http://www.google.com" id="myFrame"></iframe> 

電話:

$('a').click(function() { 
    window.top.location.href = "http://www.parenturl.com#"+$(this).attr('href'); 
}); 

呼叫時,頁面加載:

window.onload = function() { 
var loc = 'Get url from # in parent url; 
    document.getElementById('myFrame').setAttribute('src', loc); 
}); 

我都可以訪問兩個領域,但我只能編輯一個HTML。

+0

谷歌不想與iframes。 – zer00ne

+0

只是舉例url –

+0

你不能編輯子頁面,所以它是CORS允許父母的頁面位置訪問它?我很難找出一個可以編輯而另一個不能編輯的頁面。 – zer00ne

回答

0

爲了防止跨站點腳本攻擊,當iframe位於與父級不同的域上時,不允許從iframe到父級的所有通信。您可以從父級設置iframe網址。另一種方式 - 就像在將iframe中的當前url傳遞給父項一樣 - 是不可能的。

+1

嗯,當然技術上,你可以使用['postMessage'](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage)(以及適當的CORS頭文件,等等),但它需要框架內容知道要發佈什麼消息。 –

+0

你能幫我多一點嗎? –