ptrinh
ptrinh

Reputation: 131

Can I track the referrer of a new window link?

Case:

My question: Is there any way to track the referrer of a new window which was opened using target="_blank" or Javascript window.open() function?

If yes then is there any way to completely hide the referrer?

Upvotes: 13

Views: 21712

Answers (5)

Anthony
Anthony

Reputation: 37045

Referrer is in the HTTP header and so it will be available regardless of whether the window is blank or new. You can get the URL with:

console.log(document.referrer);

There are sites that use this to protect their sites from things like click-jacks, so it would be inappropriate to hide the referrer from the linked page. Browsers that allow for referrer spoofing are considered unsafe, and it tends to be patched when found.

Update

Maybe it's not as frowned upon as I thought. HTML5 has a property for <a> to not send the referrer in the HTTP headers: rel = "noreferrer".

Upvotes: 10

Dima
Dima

Reputation: 1065

IE with window.open it loses the referer. Simply use jQuery or re-write it without using jQuery:

$("<form />").attr("action", "url").attr("target", "_blank").appendTo(document.body).submit();

Upvotes: 5

Hernan
Hernan

Reputation: 424

the best way is:

    var myWindow = window.open('', 'title', 'width=500,height=350,top=100,left=100');
myWindow.location.href = 'redirect.asp';

I hope it will be useful

Upvotes: -2

tybro0103
tybro0103

Reputation: 49693

If you have control of both pages open the window like so:

var myWindow = window.open('http://my_url', 'title', 'width=500,height=350,top=100,left=100');
if(!myWindow.opener) myWindow.opener = self;

Side note: it's important to give the window a title or IE will poop itself.

In the opened window you'll have a reference to the "opener":

alert(opener.window.location);

Upvotes: 3

Dmitriy R
Dmitriy R

Reputation: 613

Did you check opener location? It worked for me if I right remember.

Upvotes: -1

Related Questions