views:

408

answers:

1

The question sums it up really...

I have a parent page with a child iframe,

How do you find out when the location of the iframe has changed, i.e. a user has clicked on a link in the iframe and the iframe is navigating to another page

I know I can bind to the onload event of the iframe like so

$("#theIframe").load(function(){
  //Do something
})

But by then it's too late... the page has already loaded...

Basically, I need to find out as soon a link is clicked or form is submitted, or whatever causes the location of the iframe to change...

It does not have to work in IE, but all the better if it does... and it does not have to be jquery either...

Thanks in advance...

+1  A: 

Try the

onbeforeunload

event on the body of the child document. It's supported by all major browsers.

Pekka
granted that's a good solution, but does that event provide me with the url of the page they are navigating to?
pǝlɐɥʞ
No, but you didn't ask for that. That is more difficult to do and not reliable. You would have to monitor the onclick and onsubmit events of every link and form in the page, and even that won't give you the target of external events like pressing the back button. There is no way to get hold of those.
Pekka