Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get parent.location.url - iframe - from child to parent

I got a page that is displayed within an iframe.

I need to get the parent.location.url with js from that page (child page).

Both sites are in different domains.

I'm trying:

alert(parent.location.url)

But I get this error:

Permission denied for http://parentdomain to get property Location.URL from http://childdomain.

???? Is it possible?

like image 824
Carlos Castillo Avatar asked Dec 10 '10 21:12

Carlos Castillo


People also ask

Can an iframe get parent URL?

location. ancestorOrigins[0] will get the parent url, but this api only works in chromium browsers (see support). this also supports nested iframes, where the bottom most child has access to the urls of each parent iframe.

Can iframe access browser cookies?

You can't share cookies across domains. You may share across subdomains. So, if your domain wrote the cookie stored on the client - whether in an iframe from other site or stored by visiting your main site, your domain should be able to access it. Otherwise - no.


2 Answers

try this:

var referrer = document.referrer; alert(referrer); 
like image 76
GianFS Avatar answered Sep 21 '22 18:09

GianFS


What you're trying to do isn't possible per se. Unless you have control over the parent page's calling of the child page. If you do, then you can call the child page with a get statement in the URL of the parent page. So if you parent page is http://parentpage.com then here's how you should call the iFrame.

<iframe src='http://childpage.com/?parent=http://parentpage.com' /> 

Then all you have to do in the child page to get the reference is get the string with

window.location.search //not sure of browser compatibility with this 

and extract the parent page's URL by grabbing it out of that string. I'd give more details, but I'm not even sure about the first question I had.

like image 31
Madison Williams Avatar answered Sep 21 '22 18:09

Madison Williams