2011-10-06 99 views
0

我試圖附加iframe中基於當前url的父頁面的url。iframe中的點擊事件

這一切都發生在同一個域上,所以我不認爲應該有任何安全問題。

編輯:

現在我的代碼如下所示:

function locator_url() { 

var iframeUrl = alert(document.getElementById("dealer-  locator").documentWindow.location.href); 

var iframeUrlSplit = iframeUrl.split('/locator/'); 

window.location.hash = '#' + iframeUrlSplit[1]; 

}; 


$(document).ready(function(){ 

    document.getElementById("dealer-locator").contentWindow.onload = locator_url(); 
}); 

現在的IFRAME默認src爲http://localhost/meade/locator/ 該網頁的iframe是爲http://localhost/meade/dealerlocator/

代碼工作對於初始頁面加載,父網址附加到localhost/meade/dealerlocator /#

但是,當我單擊iframe中的鏈接時,即使iframe中的href值具有,父url也不會更改。

父網址應該有它的hash更新類似:?

本地主機/梅亞德/ dealerlocator /#results_list.php showonly = US &標籤= US &郵編= &距離= 10 &狀態= &城市= & name =

但是,這並沒有發生。

我缺少什麼?

回答

0

你嘗試:

document.getElementById("dealer-locator").contentWindow.onload = locator_url; 
+0

將locator-url的使用更改爲locator-url(),但僅適用於初始iframe加載。如果我點擊了iframe中的鏈接,新的url似乎不會被讀取。 – Griffin

+0

當你點擊一個鏈接時,你確定iframe窗口被重新加載嗎? – David

+0

什麼會阻止它重新加載時,我點擊一個鏈接? – Griffin

0

好吧,如果你需要點擊你的iframe的事件,那麼你可以這樣來做:

<a href="http://www.car.com" onClick="parent.locator_url(); return false;">Car.com</a> 

然後locator_url()被調用父頁面。同樣適用於使用加載事件(我的意思是,加載iframe上下文中發生的事件)。