Js to solve the referer compatibility of the browser method

  • 2020-03-30 04:15:17
  • OfStack

The HTTP Header referer is basically telling people where I came from, which page I came from, and it can be used to count the source of the users who came to the site, or it can be used to prevent hotlinking. The best way to get this is js. If you get it on the server side (PHP method like $_SERVER['HTTP_REFERER']), you can fake it.

Method: using js document.referer method, we can accurately judge the real origin of web pages. At present, baidu statistics, Google ads statistics, CNZZ statistics, are using this method. Hotlinking is also very simple, js to determine the url if not the site does not show pictures.

Jump in IE using javascript, such as window.location.href = ""; Google could not fetch the HTTP referrer of the browser request if it used document.referrer, because IE cleared it

Other major browsers, like Firefox and Chrome, will keep the referrer, which means that Internet explorer will once again enjoy ministerial privileges:

The following code can solve this problem in ie:
// if the detection is Internet explorer, a referer is added manually
The idea is to sneak a link to an Internet explorer page, then automatically click on the link so the referrer can keep it.


var url = '//www.jb51.net';  
if (/MSIE (d+.d+);/.test(navigator.userAgent) || /MSIE(d+.d+);/.test(navigator.userAgent))  
{  
    var referLink = document.createElement('a');  
    referLink.href = url;  
    document.body.appendChild(referLink);  
    referLink.click();  
}  
else 
{  
    location.href = url;  


Related articles: