简体   繁体   English

你如何通过javascript包括获取Url Referer?

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

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. 如果我在谷歌搜索某些内容并点击结果(mytestsite.com),该网站的引用将是谷歌搜索的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请求的服务器没有办法知道它来自从谷歌搜索?

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. ...并在您的JS文件请求中将其传递给服务器。 Several ways to do this...here's one: 有几种方法可以做到这一点......这里有一个:

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

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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM