The javascript operation referer parses in detail

  • 2020-03-30 02:18:03
  • OfStack

The importance of the Referrer
The HTTP request has a referer header that indicates the source of the current traffic, the reference page. For example, at click a link to the home page of, and the referrer is In Javascript, we can get the same information through the document.referrer. With this information, we can know from what source the visitor came to the current page. This is very important for Web Analytics, which can tell us the distribution of traffic from different channels, as well as the keywords searched by users, etc., all obtained by analyzing the referrer information.

However, for a variety of reasons, sometimes the referrer you read in Javascript is an empty string. Here's a summary of the situations where the referrer is lost.

Modify the Location object to navigate the page
The Location object is a very useful object for page navigation. Because it allows you to change only part of the Url. For example, switch from cn domain name to com domain name, other parts unchanged:

window.location.hostname = ""; 

However, changing the way the page navigates from Location causes the Referrer to be lost under IE.

Returns an empty string under IE5.5+

Chrome3.0+, Firefox3.5, Opera9.6, Safari3.2.2 all returned to the source opens a new window

<a href="#" onclick="'')"> access Google</a> 

Clicking on this link will open the Google website in a new window, and we can see the referrer sent by typing the following js code in the address bar.


Test results:

Returns an empty string under IE5.5+

Chrome3.0+, Firefox3.5, Opera9.6, Safari3.2.2 all returned to the source

If we jump this way under the same domain name, we can get the lost referrer information by accessing the windoww.opener object. The code is as follows:

<script type="text/javascript"> 
    var referrer = document.referrer; 
    if (!referrer) { 
        try { 
            if (window.opener) { 
                //Under IE if cross - domain will throw a permission exception
                //Safari and Chrome window.opener. Location doesn't have any properties
                referrer = window.opener.location.href; 
        catch (e) {} 

Cross domain words have no way ~


Maxthon2.5.2, Firefox FireGesture plug-in, Chrome3.0+, Opera9.6, Safari3.2.

Click on the Flash internal link
When you click on Flash to get to another site, the Referrer gets messy.

Under IE, the value read by the client Javascript document.referrer is empty, but if you look at it with traffic monitoring software, you will find that the Referer header in the HTTP request actually has a value, which may be a Bug in the IE implementation. Also, this value points to the address of the Flash file, not the source web page.

When you click Flash under Chrome4.0 to reach a new window, the Referrer is also the address that points to the Flash file, not the source page.

Chrome3.0 is the same as Safari3.2 in that it loses Referrer information.

In Opera, as in Firefox, the Referrer's value is the address of the source page.

HTTPS jumps to HTTP
When you jump from an HTTPS site to an HTTP site, the browser does not send the referrer. The behavior of the major browsers is the same.

For example, when using Google Reader or Gmail over HTTPS and clicking on a link to another site, there is technically no difference between such access and a user typing in a url.

The effect of Referrer loss on AD traffic monitoring
If the Referrer is lost, Web Analytics loses an important piece of information, especially for AD traffic, and it's impossible to know the actual source. At present, many websites that use Google Adsense advertisement in China use to open the advertising link, so the Referrer will be lost under IE. As we know, IE is the browser with the largest market share at present, so its influence is very big. Many traffic statistics tools therefore classify this traffic as "direct traffic," which is equivalent to the user typing in a url.

In such cases, you need to have the advertiser attach specific tracking parameters to the Url of the landing page when they place their ads.

For example, for a Flash AD, the Url reached after clicking is In order to monitor which channel this traffic comes from, we can change the landing Url of this advertisement to SRC =sina, similar to this way, and then in the landing page using Javascript code to extract this SRC parameter, so that you can get the advertising source information.

When Google Adwords are launched, the background system has an option to "automatically tag". When this option is enabled, Google will automatically add a gclid parameter when generating the landing page Url of all ads, which can integrate the data of Google Analytics background and Adwords background. In this way, we can know which advertising traffic corresponds to which advertising series, which advertising sources and advertising keywords and so on. And the above mentioned ideas are actually similar. It's just that Google automatically modifies the Url for you.

IE drop the referer for the empty solution
If you jump in the window.location.href mode under IE, the referer value is null. The referer for a jump inside the tag is not empty. Therefore, this IE problem can be solved by the following code

function gotoUrl(url){ 
         var gotoLink = document.createElement('a'); 
         gotoLink .href = url; 
         gotoLink .click(); 
       window.location.href = url; 

Browsers are not allowed to bring the referer with them when accessing a link
When we click a link from one site to another, the browser adds a Referer value to the header to identify the page from which the visit originated. But this kind of signage can leak the user's privacy, and sometimes I don't want others to know where I clicked in. Is there a way to make the browser not send the Referer?

Open a new window, which is equivalent to target="_blank":

function open_window(link){  
    var arg = 'u003cscriptu003elocation.replace("'+link+'")u003c/scriptu003e';';', arg); 

Redirect to a connection, equivalent to target="_self":

function redirect(link){  
    var arg ='u003cscriptu003etop.location.replace("'+link+'")u003c/scriptu003e'; 
    var iframe = document.createElement('iframe'); 

Related articles: