开发者

Can I track the referrer of a new window link?

Case:

  • site A contains a link that opens site B in new window.
  • The script in site B is trying to get the site A url.

My question: Is there any way to track the referrer of a new window which was opened using targ开发者_如何学Goet="_blank" or Javascript window.open() function?

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


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".


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();


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);


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜