问题描述
假设我无法控制iframe中的内容,是否有任何方法可以通过父页面检测到src更改?某种onload可能吗?
Assuming I have no control over the content in the iframe, is there any way that I can detect a src change in it via the parent page? Some sort of onload maybe?
如果iframe src和以前一样,我最后的办法就是进行1秒的间隔测试,但是做这个hacky解决方案会哎呀。
My last resort is to do a 1 second interval test if the iframe src is the same as it was before, but doing this hacky solution would suck.
如果有帮助,我正在使用jQuery库。
I'm using the jQuery library if it helps.
推荐答案
您可能需要使用 onLoad
事件,如下例所示:
You may want to use the onLoad
event, as in the following example:
<iframe src="http://www.google.com/" onLoad="alert('Test');"></iframe>
只要iframe中的位置发生变化,警报就会弹出。它适用于所有现代浏览器,但可能无法在IE5和早期Opera等一些非常老的浏览器中使用。 ()
The alert will pop-up whenever the location within the iframe changes. It works in all modern browsers, but may not work in some very older browsers like IE5 and early Opera. (Source)
如果iframe在父的同一域中显示某个网页,您就可以使用 contentWindow访问该位置。 location
,如下例所示:
If the iframe is showing a page within the same domain of the parent, you would be able to access the location with contentWindow.location
, as in the following example:
<iframe src="/test.html" onLoad="alert(this.contentWindow.location);"></iframe>
这篇关于iFrame src改变事件检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!