Is it possible to tell if the src of an iFrame has changed?

倾然丶 夕夏残阳落幕 提交于 2019-12-13 19:29:18

问题


Is it possible to tell if the source of an iFrame has redirected the client to another page? And if so, tell what page the client got redirected to?


回答1:


onload event

like

<iframe src="http://www.mydomain/" onLoad="alert('fire');"></iframe>

The alert will pop-up whenever the location within the iframe has changed. check it out here also

Here is a question explaining how to get current location of an iframe




回答2:


The src attribute won't change if the iframe navigates to a new page, but the location of the iframe's contentWindow will.

If you start the iframe in a page on your own domain you can use the iframes onload event to read iframe.contentWindow.location.href.

Use a try-block, and if it fails because of a cross domain call, you can return that the iframe is now in some remote site.

If it returns a value you can return the url on your site the iframe has navigated to.




回答3:


Allright, so I've been fiddeling with this script for a while now and I think I've reached a conclusion which I think is worth sharing. Cross-domain is pretty strict, and as far as I know there is no way of telling what url the xmlhttprequest object got passed on to. I did however notice a solution on another site where they suggest using a hidden iFrame (I'm referring to the link in @ayush's answer).



来源:https://stackoverflow.com/questions/4977930/is-it-possible-to-tell-if-the-src-of-an-iframe-has-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!