Detect if the url in an iFrame has changed

2020-05-01 08:38发布

问题:

Is there anyway to detect if a user clicks a link inside of an iframe if the iframe is not on the same domain as the parent page?

EDIT: I don't have access to the iframed page. I only own the parent window.

回答1:

No. Due to the javascript same-origin policy, you cannot access any members of the iframe's DOM if the iframe did not serve from the same domain as the page on which your javascript is running.



回答2:

if (window.domain !== top.domain) {
  // they are not the same
}


回答3:

Try this.

var isInSameDomain = (window.location.host == window.parent.location.host);