javascript 我可以跟踪新窗口链接的引荐来源吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6669463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I track the referrer of a new window link?
提问by ptrinh
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.
- 站点 A 包含在新窗口中打开站点 B 的链接。
- 站点 B 中的脚本正在尝试获取站点 A 的 url。
My question: Is there any way to track the referrer of a new windowwhich was opened using target="_blank"
or Javascript window.open()
function?
我的问题:有没有办法跟踪使用target="_blank"
或 Javascriptwindow.open()
函数打开的新窗口的引用者?
If yes then is there any way to completely hide the referrer?
如果是,那么有什么方法可以完全隐藏推荐人?
回答by Anthony
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:
Referrer 位于 HTTP 标头中,因此无论窗口是空白还是新窗口,它都可用。您可以通过以下方式获取 URL:
console.log(document.referrer);
There are sites that use this to protect their sites from things like click-Hymans, so it would be inappropriate to hide the referrer from the linked page. Browsers that allow for referrer spoofingare 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"
.
也许它并不像我想象的那样皱眉。HTML5 具有<a>
不发送 HTTP 标头中的引荐来源网址的属性:rel = "noreferrer"
。
回答by Dima
IE with window.open it loses the referer. Simply use jQuery or re-write it without using jQuery:
IE 与 window.open 它失去了引用。只需使用 jQuery 或重新编写它而不使用 jQuery:
$("<form />").attr("action", "url").attr("target", "_blank").appendTo(document.body).submit();
回答by tybro0103
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.
旁注:给窗口一个标题很重要,否则 IE 会自己拉屎。
In the opened window you'll have a reference to the "opener":
在打开的窗口中,您将获得对“opener”的引用:
alert(opener.window.location);
回答by Dmitriy R
Did you check opener location? It worked for me if I right remember.
你检查开瓶器的位置了吗?如果我没记错的话,它对我有用。
回答by Hernan
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
我希望它会很有用