Equivalent for 'window.location.ancestorOrigins' in FF and IE
Asked Answered
P

5

17

Following is Google Chrome version for getting parent window's URL from iFrame.

window.location.ancestorOrigins;

I'm looking for equivalent of above statement for FireFox and IE. Is it possible to achieve it.

Tried with document.referrer also giving the iFrame Url only.

Pythagoras answered 12/1, 2016 at 11:44 Comment(3)
possible duplicate of thisSkeleton
Not a duplicate, @Siddharth, that link only concerns parent frame, this question is about a property that shows all parent frames.Frodeen
For what it's worth, Mozilla have deliberately chosen not to implement the ancestorOrigins API in Firefox for privacy reasons. There is an ongoing discussion on the whatwg group regarding changing the API to make use the referrer policy.Fluid
K
6

Unfortunately, FireFox and IE do not have an equivalent to ancestorOrigins.

The best you can do with regards to getting the parent URL is document.referrer and unfortunately if you are dealing with iframes, this means you may not be able to get to the outside page and get the true URL of the webpage.

Kaltman answered 26/6, 2019 at 20:30 Comment(0)
S
1

This is a known bug in Firefox

Snavely answered 21/7, 2022 at 11:15 Comment(0)
S
1

I do something like this:

function getAncestorOrigins() {
  if (location.ancestorOrigins !== undefined) {
    return [...location.ancestorOrigins];
  }
  const urls = [];
  let parentWin = window;
  while (parentWin !== window.top) {
    if (parentWin.document.referrer) {
      try {
        const url = new URL(parentWin.document.referrer);
        urls.push(url.origin);
      } catch (e) {
        // console.error
      }
    }
    // @ts-ignore
    parentWin = parentWin.parent;
  }
  return urls;
}
Snavely answered 21/7, 2022 at 11:45 Comment(1)
you cant do that in crossdomain iframe :/Monolingual
T
0

I found this alternative solution. This works for me...

var url = window.location != window.parent.location ? 
           document.referrer :
           document.location.href;
Toed answered 8/4, 2022 at 10:4 Comment(0)
O
0

One way can be by checking if window.parent[0].top.document exists.

If you don't get an error when querying this object assume it's in the same domain, otherwise assume it's in different domains.

var origins = location.ancestorOrigins;
if ( ! origins ) {
  origins = { "length": 1 };
  try {
    if ( window.parent[0].top.document )
      // Chrome & Firefox (same domain)
      origins[0] = location.origin;

  } catch(err) {
    // Firefox (different domains CORS policy)      
    origins[0] = "*"; // force any domain
  };
};
console.log( origins, location.origin )
if ( origins[ origins.length -1 ] == location.origin ) {
  // same domain
  alert("Hello word!");

} else {
  // different domains
  var message = 'alert("Hello word!");';
   window.parent.postMessage( message, origins[ origins.length -1 ] );
};
Onia answered 25/7, 2023 at 20:2 Comment(1)
you can't execute in the iFrame for cross domain. have you tried it in FF?Inalienable

© 2022 - 2024 — McMap. All rights reserved.