Click event in iframe

廉价感情. 提交于 2019-12-13 04:00:38

问题


I'm trying to append the url of the parent page an iframe is located in based on the current url within the iframe.

This is all taking place on the same domain, so I don't think there should be any security issues.

EDIT:

My code now looks like the following:

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(); 
});

Now the default src for the iframe is http://localhost/meade/locator/ The page the iframe is on is http://localhost/meade/dealerlocator/

The code works for the initial page load, the parent url is appended to localhost/meade/dealerlocator/#

However, when I click a link inside the iframe the parent url doesn't change, even though the href value in the iframe has.

The parent url should have it's hash updated to something like:

localhost/meade/dealerlocator/#results_list.php?showonly=US&tab=US&zip=&distance=10&state=&city=&name=

But that's not happening.

What am I missing?


回答1:


Did you try:

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



回答2:


Well, if you need click events in your iframe, then you could do it this way:

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

Then locator_url() gets called in the parent page. Same goes for using load events (I mean, load events that occur in the iframe context).



来源:https://stackoverflow.com/questions/7680778/click-event-in-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!