2011-10-06 102 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上下文中发生的事件)。