Read parent location from frame (different domains)

How can an iframe read the URL of its parent location , the domains of the parent window and iframe are different?

I know that the “general” answer to this question is that because of domain conflicts, browsers do not allow cross-domain callbacks. So the following will not work :

parent.location.href

But maybe someone can think out of the box and suggest something similar to Intercepting messages between documents ?

+3
source share
2 answers

I understood!

My solution is as follows:

1. Use the onload attribute with the frame

<iframe src="http://website.com/" onload="src+='#'+document.location"></iframe>

2. Changes to the hash of the monitor in the frame

var referrer = '?';
window.onhashchange = function() {
  var ref = document.location.hash;
  if(ref && ref.length > 1) {
    var t = ref.split('#');
    ref = t[1];
  }
  referrer = ref;
};

"": -)

, onhashchange.

0

, document.referrer? , , iframe.

+1

Source: https://habr.com/ru/post/1793315/


All Articles