JavaScript Operation Referer Parsing

Source: Internet
Author: User
Tags http request

  This article is mainly on the JavaScript operation Referer A detailed introduction, the need for friends can come to the reference, I hope to help you.

Referrer importance HTTP requests have a Referer message header that indicates the source reference page for the current traffic. For example, click on a link on the www.sina.com.cn/sports/to reach the cctv.com home page, then referrer is www.sina.com.cn/sports/. In JavaScript, we can get the same information through Document.referrer. With this information, we can find out what channels visitors come to the current page. This is very important for the web analytics, which can tell us the distribution of the flow of different channels, as well as user search keywords, and so on, are through the analysis of this referrer information to obtain.   But for a variety of reasons, sometimes the referrer read in JavaScript is an empty string. Here's a summary of the circumstances in which referrer will be lost.   Modify Location object for page navigation location object is a very useful object for page navigation. Because he allows you to change only one part of the URL. For example, from the CN domain name switch to the COM domain name, the other parts are unchanged:     code as follows: Window.location.hostname = "example.com";    But, By modifying the location of the page navigation method, will lead to the loss of referrer under IE.   ie5.5+ returns an empty string   chrome3.0+,firefox3.5,opera9.6,safari3.2.2 all normal return to the source page   window.open way to open a new window example:   & nbsp Code as follows: <a href= "#" onclick= "window.open (' http://www.google.com ')" > Access google</a>    Click this link will open the Google website in the new window, we enter the following JS code in the address bar to be able to see sends the referrer.   Code as follows: Javascript:alert (document.referrer)     test results:   ie5.5+ Returns an empty string of   chrome3.0+, Firefox3.5,opera9.6,safari3.2.2 Normal return of the source page   if it is under the same domain name jump in this way, then we can access the Windoww.opener object to get lost referrer information. The code is as follows:   code is as follows: <script type= "Text/javascript" >      var referrer = document.referrer;  & nbsp   if (!referrer) {          Try {              if (window Opener) {                 //IE under if cross-domain throws permission exception         &NBS P        //Safari and Chrome window.opener.location no attributes                   referrer = window.opener.location.href;             }    &NBS P    }           catch (e) {}     }  </script>   cross-domain   Mouse drag and drop to open a new window is now very popular user habits, many browsers are built-in or can be plug-ins to support the mouse drag-and-drop browsing. But the page that opens in this way, basically all loses referrer. And, in this case, it is not possible to use the Window.opener way to get the lost referrer.   Tested:   MAXTHON2.5.2,FIREFOx's Firegesture plugin, chrome3.0+,opera9.6,safari3.2.   Click on Flash internal link click Flash to reach another site, the referrer situation is more messy.   IE, The value read from the client JavaScript document.referrer is empty, but if you look at it using the traffic monitor software, you will find that the Referer message header in the HTTP request is actually a value, which may be a bug implemented by IE. Also, this value points to the address of the Flash file, not the address of the source page.   Chrome4.0 Click Flash to reach a new window, referrer is also the address of the Flash file point, not the address of the source page.   Chrome3.0 and Safari3.2 are the same, will be lost referrer information.   Opera, like Firefox, referrer values are the addresses of the source pages.   HTTPS jumps to HTTP from the website of HTTPS to the HTTP site when the browser is not sent referrer. The behavior of the big browsers is the same.   For example, when we use Google Reader or gmail under HTTPS, click on a link to another site, so technically, such access is not the same as the user's direct access to the Web site. The impact of   referrer loss on advertising flow monitoring referrer if lost, the Web analytics will lose an important part of the information, especially for advertising traffic, you can not know the actual source. At present, many of the domestic use of Google AdSense ads site, have used the window.open way to open the ads link, so IE will lose referrer, and we know that IE is currently the largest market share of the browser, so its impact is very large. A lot of traffic statistics tools will therefore put this part of the traffic into the "direct traffic", and users directly type the URL equivalent.   For such situations, advertisers need to add specific tracking parameters to the URL of the landing page when they advertise.   For example, a flash ad, click on the arrival of the URL is http://www.example.com/, in order to monitor the flow from which channel, we can modify the launch of the landing URL, changed to http://www.example.com /?src=sina, like this side, and then extract the SRC parameter using JavaScript code in the landing page, so you can get the source information of the ad.   In the launch of Google AdWords, the background system has an "auto tag" option, when this option is enabled, Google in the generation of all Ads landing page URL, will automatically add a gclid parameter, this parameter can Google Analytics background and AdWords ads backstage data integration. This will be able to know the advertising flow corresponding to which advertising series, which advertising sources and advertising keywords and other information. And the ideas mentioned above are actually similar. But Google automatically helped you make changes to the URL.   IE under the referer for empty solution in IE under the window.location.href way to jump, Referer value is empty. And in the tags inside the jump words referer will not be empty. So, the following code can be used to resolve this IE problem code as follows: function Gotourl (URL) {       if (window. VBArray) {           var gotolink = document.createelement (' a ');        & nbsp  gotolink. href = url;           document.body.appendchild (gotolink);    & nbsp      gotolink. Click ();       }else{         window.location . href = url;       }   }    prohibit browsers from accessing links without referer when we click a link from one website to another page, The browser adds a Referer value to the header to identify the source page for this visit. But this kind of identification may leak the user's privacy, and sometimes I don't want anyone else to knowWhere do I click in, can have the means to let the browser do not send Referer?   uses the new HTML5 solution and uses rel= "Noreferrer" to declare the properties of the connection to be noreferrer and only chrome4+ support is currently available. • Use the middle page, but actually send referrer, such as using Google's connection to turn, noreferrer.js. • Use JavaScript protocol links to relay, see the instructions below.   Open a new window, equivalent to target= "_blank":   code as follows: function Open_window (link) {      var arg = ' u003cscriptu003 Elocation.replace ("' +link+ '") u003c/scriptu003e ';      window.open (' javascript:window.name; ', ARG);  }  </CODE>    turn to a connection, equivalent to target= "_self":   code as follows: function redirect (link) {      var arg = ' U003cscriptu003etop.location.replace ("' +link+ '") u003c/scriptu003e ';      var iframe = Document.createelement (' iframe ');      iframe.src= ' javascript:window.name;       iframe.name=arg;      Document.body.appendChild (iframe); }  </code >   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.