点击iframe中的事件

时间:2011-10-06 21:39:08

标签: javascript jquery iframe

我正在尝试根据iframe中的当前网址附加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/

代码适用于初始页面加载,父URL附加到localhost / meade / dealerlocator /#

但是,当我点击iframe内的链接时,即使iframe中的href值有,也不会更改父网址。

父网址应该将其哈希更新为:

本地主机/米德/ dealerlocator /#results_list.php showonly = US&安培;标签= US&安培;拉链=安培;距离= 10安培;状态=安培;城市=安培;名称=

但那并没有发生。

我错过了什么?

2 个答案:

答案 0 :(得分:0)

你有没有尝试过:

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

答案 1 :(得分:0)

好吧,如果您需要在iframe中点击活动,那么您可以这样做:

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

然后在父页面中调用locator_url()。使用加载事件也是如此(我的意思是,加载iframe上下文中发生的事件)。

相关问题