<script type="text/javascript">
var url = document.referrer;
document.write(url);
  </script>

建立文件a.asp:
<%
Dim from
from = Request.ServerVariables("HTTP_REFERER")
Response.write "访问来源是:" & from
%>

<script language="javascript">
var from = document.referrer;

if (from != '') alert("访问来源是:" + from);
</script>
<br/>
11111111111

在浏览器输`http://localhost/a.asp`(根据实际修改访问路径),看到效果如下:

访问来源是:
11111111111

会看到并没有获取到访问来源。然后建立一个b.asp文件: `<a href="a.asp">dfd</a>` 

在浏览器输`http://localhost/b.asp`(根据实际修改访问路径),点击超链接,看到效果如下:

访问来源是: http://localhost/b.asp
11111111111

这就说明获取到了网页的访问来源。从以上步骤看出,直接在浏览器地址栏输入url是没有访问来源的,从页面的超链接等地方打开的页面就可以获取访问来源的url。上面给出了在js里用document.referrer得到访问来源,在asp里用Request.ServerVariables("HTTP_REFERER")获得访问来源。其实在jsp里也是类似的,用request.getHeader("Referer")。
获取网页的访问来源又有什么用途呢?一是可以跟踪记录从哪些网站、网页上访问了自己的站点或者页面;二是可以记住刚才访问的页面,可以跳转回去;还有一个很重要的用途,就是防盗链。一些网站的资源如图片、视频等,可能被链到其它网站上,或者黑客频繁的访问某些资源,把网站搞瘫痪,于是一些网站用了防盗链技术。一种简单的防盗链方法就是获取访问的url,比对是否是自己的url或者经授权的url,不符合条件的就不让访问,或者显示一些提示信息。
道高一尺,魔高一丈。访问的url是很容易被伪造的,我们再做一个c.asp:

<%
Dim http
set http = server.createobject("Microsoft.XMLHTTP")
http.open "GET","http://localhost/a.asp",false
http.setRequestHeader "Referer","http://www.baidu.com/"
http.send()
Response.write http.Responsetext
%>

然后在浏览器输入 `http://localhost/c.asp` (根据实际修改访问路径),页面上看到如下内容:

访问来源是: http://www.baidu.com/

var args = {

mySite:"localhost",

subSite:"子网站,比如移动端: m.xxx.com"

}

var index = (function(a) {

var c = "";

var d = args.mySite;

a(function() {

var g = function() {

return function(k, l, j) {

var h = j * 24 * 60 * 60 * 1000;

var i = new Date();

i.setTime(i.getTime() + h);

var e = "; expires=" + i.toGMTString();

document.cookie = k + "=" + escape(l) + e + "; path=/"

}

};

try {

if (document.referrer.indexOf(d) <= 0 || document.referrer.indexOf(d) > 10) {

g().call(window, "regCompanyName", document.referrer.substring(0, 40) + "||" + window.location.href, 30)

}

} catch (f) {

}

});

var b = function() {

try {

if (getCookie) {

return getCookie

}

} catch (f) {}

return function(e) {

if (document.cookie.length > 0) {

c_start = document.cookie.indexOf(e + "=");

if (c_start != -1) {

c_start = c_start + e.length + 1;

c_end = document.cookie.indexOf(";", c_start);

if (c_end == -1) {

c_end = document.cookie.length

}

return unescape(document.cookie.substring(c_start, c_end))

}

}

return null

}

};

})(jQuery);

//媒体

var media = "";

//媒体细分

var mediaSubpide = "";

//注册渠道。(直接赋值)

var registerChannel = "";

//终端,根据用户使用设备判断

var terminal = "";

//数据来源,根据cookie确定

var dataSource = "";

//当前链接

var localUrl = window.location.href;

var userChannel = {

//媒体

Media: function(channelInfo){

channelInfo=channelInfo.toLowerCase();

if(channelInfo.indexOf("||") == 0 && (channelInfo.indexOf(args.mySite) >= 0)){

//媒体 无法判断(如直接搜索)

media = "网址打开或站内跳转"; //媒体

mediaSubpide = ""; //媒体细分

// 无法获取referrer的来源的情况有:

//1、个别平台外链使用noreferrer;

//2、https跳转http;

//3、网页重定向...

//可在外链中植入可判定的参数如www.xxx.com&tag=zhihu

if(channelInfo.indexOf("zhihu")>=0){

media = "知乎";

mediaSubpide = "";

}

}else if(channelInfo.indexOf("baidu.com") != -1){

media = "百度"; //媒体

if(channelInfo.indexOf('utm_medium=cpc') != -1){

mediaSubpide = "sem"; //媒体细分

}else if(channelInfo.indexOf('utm_medium=cpc') == -1){

mediaSubpide = "seo"; //媒体细分

}else{

mediaSubpide = "其它"; //媒体细分

}

}else if(channelInfo.indexOf('haosou.com') != -1 || channelInfo.indexOf('so.com') != -1){

media = "好搜"; //媒体

mediaSubpide = "无法判断"; //媒体细分

}else if(channelInfo.indexOf('sogou.com') != -1){

media = "搜狗"; //媒体

mediaSubpide = "无法判断"; //媒体细分

}else if(channelInfo.indexOf('sm.cn') != -1){

media = "神马"; //媒体

mediaSubpide = "无法判断"; //媒体细分

}else if(channelInfo.indexOf('bing.com') != -1){

media = "必应"; //媒体

mediaSubpide = "无法判断"; //媒体细分

}else if(channelInfo.indexOf('google.com') != -1){

media = "google"; //媒体

mediaSubpide = "无法判断"; //媒体细分

}else if(channelInfo.indexOf('douban.com') != -1){

media = "豆瓣"; //媒体

mediaSubpide = "无法判断"; //媒体细分

}else if(channelInfo.indexOf("zhihu.com") != -1){

media = "知乎";

mediaSubpide = "无法判断";

}

else if(channelInfo.indexOf("toutiao") != -1){

media = "今日头条";

mediaSubpide = "无法判断";

}

else{

media = "其它"; //媒体

mediaSubpide = "无法判断"; //媒体细分

}

var mediaInfo=new Array(media,mediaSubpide);

return mediaInfo;

},

//终端

Terminal: function(){

if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))){

terminal = "移动端";

return terminal;

}else{

terminal = "PC端";

return terminal;

}

},

//注册渠道。通过判断当前页面url获取

RegisterChannel: function(){

if(localUrl.indexOf(args.mySite)!= -1 ){

registerChannel = args.mySite+"网站注册";

return registerChannel;

}else if(localUrl.indexOf(args.subSite)!= -1){

registerChannel = args.subSite+"网站注册";

return registerChannel;

}else{

registerChannel = "未知";

return registerChannel;

}

}

};

//通用设置cookie方法

function setCookie(name, value, days) {

var cookieTime = days * 24 * 60 * 60 * 1000;

var d = new Date();

d.setTime(d.getTime() + cookieTime);

var cookieExpires = "; expires=" + d.toGMTString();

document.cookie = name + "=" + escape(value) + cookieExpires + "; path=/";

}

//通用读取cookie方法

function getCookie(name) {

var strCookie = document.cookie;

var arrCookie = strCookie.split("; ");

for (var i = 0; i < arrCookie.length; i++) {

var arr = arrCookie[i].split("=");

if (arr[0] == name)

return unescape(arr[1]);

}

return "";

}

//排除站内跳转的情况,以免window.name被错误覆盖

if(document.referrer.indexOf(args.mySite)<=0){

window.name=document.referrer.substring(0,40)+"||"+window.location.href;

}

//使用案例

dataSource = getCookie('regCompanyName');

if(!dataSource || ""== dataSource || typeof(dataSource)== "undefined") {

//当cookie无法使用时,优先使用window.name

dataSource = (window.name) ? window.name : "";

}

var judgeMedia = userChannel.Media(dataSource);

var judgeTerminal = userChannel.Terminal();

var judgeRegisterChannel = userChannel.RegisterChannel();

console.log(judgeMedia+' ',judgeTerminal+' ',judgeRegisterChannel+' ');