Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between document.referrer and window.parent.location.href

Here's the situation: there is a site, and it belongs to the client, so it's not on my domain, lets say client.com.

On this site there is an iframe, the source of this iframe is a simple js code, which loads another js (client.js) - this code is on my domain.

What I need to do is to get the exact url of the page where the iframe is. So now I'm trying to fugure out the difference between document.referrer and window.parent.location.href with no luck.

Both give me exactly what I need, but I can't realize what is more reliable? Is there a situation, where one will work and another won't?

like image 446
k102 Avatar asked May 07 '13 07:05

k102


People also ask

What is window location href?

Window Location Href The window.location.href property returns the URL of the current page.

How do you set a Windows href location?

href = "https://www.example.com"; // Assigns a new URL to the current window. window. location. assign("https://www.example.com"); // Replaces the location of the current window with the new one.

Can we change document referrer?

You can not change the REFERRER property.


2 Answers

document.referrer gives you the URI of the page that linked to the current page. This is a value that's available for all pages, not just frames.

window.parent gives you the parent frame, and its location is its URI.

If you want to find the URI of the parent frame, then use window.parent.location.

like image 138
deceze Avatar answered Sep 18 '22 04:09

deceze


The main difference is that the document.referrer will point to the page which linked to the current page inside the iframe. If your iframe content contain links, which allows to navigate through a few pages, then only the first page loaded inside the iframe will have parent frame URI as document.referrer. Each page loaded by clicking link inside the iframe will have the uri of the page containing link in the document.referrer.

At the same time window.parent.location will always contain the URI of the page in parent window, but it will be accessible only if the site origin is the same. Read about relaxing site origin policy to see what should be done on both your and your client sites, so you can access the data.

That being said, I would rather give your client something like a service key or token, which will authorize his site to use your iframed app, and which will authenticate the caller as your client, so you can know that the call is from his site.

like image 24
SWilk Avatar answered Sep 20 '22 04:09

SWilk