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

2019-07-25 04:36发布

问题:

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).