Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting src/location change in a iframe object

I have an iframe object pointing to a specific page. For example,

<iframe src="http://en.wikipedia.org/wiki/Special:Random"></iframe>

I want to have an alert whenever the location of the iframe changes because the user has clicked a link inside it.

Doing onLoad="alert(this.ContentWindow.location.href);" yields nothing.

Doing onLoad="alert(this.src);" yields the initial src (../wiki/Special:Random) no matter what the user has clicked.

The user will stay within the same domain, so the Same Origin policy is not violated.

like image 323
carriwitchet Avatar asked Aug 12 '13 19:08

carriwitchet


1 Answers

Use correct case in "ContentWindow", it's supposed to be "contentWindow".

<iframe src="your initial URL" onload="alert(this.contentWindow.location.href)" /> 

works.

like image 149
Yuriy Galanter Avatar answered Oct 12 '22 00:10

Yuriy Galanter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!