javascript 检测 iframe 对象中的 src/location 变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18195542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting src/location change in a iframe object
提问by carriwitchet
I have an iframe object pointing to a specific page. For example,
我有一个指向特定页面的 iframe 对象。例如,
<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.
我想在 iframe 的位置发生更改时收到警报,因为用户单击了其中的链接。
Doing onLoad="alert(this.ContentWindow.location.href);"
yields nothing.
做事onLoad="alert(this.ContentWindow.location.href);"
一无所获。
Doing onLoad="alert(this.src);"
yields the initial src (../wiki/Special:Random) no matter what the user has clicked.
这样onLoad="alert(this.src);"
的产量初始SRC(../wiki/Special:Random)无论用户点击了什么。
The user will stay within the same domain, so the Same Origin policy is not violated.
用户将留在同一个域中,因此不会违反同源策略。
采纳答案by Yuriy Galanter
Use correct case in "ContentWindow", it's supposed to be "contentWindow".
在“ContentWindow”中使用正确的大小写,它应该是“contentWindow”。
<iframe src="your initial URL" onload="alert(this.contentWindow.location.href)" />
works.
作品。