carriwitchet
carriwitchet

Reputation: 121

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.

Upvotes: 10

Views: 21649

Answers (1)

Yuriy Galanter
Yuriy Galanter

Reputation: 39777

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

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

works.

Upvotes: 5

Related Questions