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

僤鯓⒐⒋嵵緔 提交于 2019-11-29 00:29:53

问题


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.

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?


回答1:


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.




回答2:


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

document.referrer

...and pass it along to the server in your request for the JS file. 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>


来源:https://stackoverflow.com/questions/2157396/how-do-you-get-the-url-referer-via-a-javascript-include

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!