您如何通过 javascript 包含获取 Url Referer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2157396/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:59:57  来源:igfitidea点击:

How do you get the Url Referer via a javascript include?

javascriptreferrer

提问by puffpio

If I search for something on google and click on a result (mytestsite.com), the referer to that site will be URL of the google search.

如果我在 google 上搜索某些内容并单击结果 (mytestsite.com),则该站点的引用将是 google 搜索的 URL。

Now on that site, there is a JS file include that is used for tracking purposes..however the referrer to that JS file request is mytestsite.com...is there no way for the server handling the JS request to know that it originated from a google search?

现在在该站点上,有一个用于跟踪目的的 JS 文件包含......但是,该 JS 文件请求的引用者是 mytestsite.com......服务器处理 JS 请求是否无法知道它源自从谷歌搜索?

采纳答案by Andy E

A script tag will always refer to the document that is sourcing it. If you're doing something special on the server you might want to consider using a session or cookies.

脚本标签将始终引用它的来源文档。如果您在服务器上做一些特别的事情,您可能需要考虑使用会话或 cookie。

回答by droo

I'm a little unclear on what you are trying to do, but you can grab the referrer with JavaScript using:

我有点不清楚您要做什么,但您可以使用 JavaScript 获取引用:

document.referrer

...and pass it along to the server in your request for the JS file. Several ways to do this...here's one:

...并在您对 JS 文件的请求中将其传递给服务器。有几种方法可以做到这一点......这是一个:

<script>
 var e = document.createElement("script");
 e.src = 'someJSfile.js?referrer='+document.referrer;
 e.type="text/javascript";
 document.getElementsByTagName("head")[0].appendChild(e);
</script>