检测iframe对象中的src /位置更改

car*_*het 8 html javascript css

我有一个指向特定页面的iframe对象.例如,

<iframe src="http://en.wikipedia.org/wiki/Special:Random"></iframe>
Run Code Online (Sandbox Code Playgroud)

我想在iframe的位置发生变化时发出警报,因为用户点击了其中的链接.

这样做onLoad="alert(this.ContentWindow.location.href);"的产量没有.

这样onLoad="alert(this.src);"的产量初始SRC(../wiki/Special:Random)用户点击不管.

用户将保持在同一域中,因此不会违反同源策略.

Yur*_*ter 5

在“ContentWindow”中使用正确的大小写,它应该是“contentWindow”。

<iframe src="your initial URL" onload="alert(this.contentWindow.location.href)" /> 
Run Code Online (Sandbox Code Playgroud)

作品。

  • 您无法访问与父窗口不在同一域或协议内的网站的“contentWindow.location”。特别是在谷歌浏览器上。 (6认同)
  • @HashemQolami 我只能假设显示的 URL 是一个随机示例,或者 OP 适用于维基百科:) (2认同)