Referer parsing for javascript operations

Source: Internet
Author: User

This article mainly introduces the javascript referer operation in detail. If you need it, please refer to it and hope to help you.

Referrer importance an HTTP request contains a referer header to indicate the source reference page of the current traffic. For example. In Javascript, we can use document. referrer to obtain the same information. With this information, we can know the channel through which visitors come to the current page. This is very important for Web Analytics, which can tell us the distribution of traffic brought about by different channels, as well as keywords for user search, all are obtained by analyzing the referrer information. However, for various reasons, sometimes the referrer read in Javascript is a null string. Next we will summarize the situations in which referrer will be lost. Modifying the Location object for page navigation Location object is a very practical object for page navigation. Because it allows you to change only some of the URLs. For example, to switch from a cn domain name to a com domain name, the other part remains unchanged: the code is as follows: window. location. hostname = "example.com"; however, modifying the Location for page navigation results in the loss of Referrer in IE. IE5.5 + returns the null strings Chrome3.0 +, Firefox3.5, Opera9.6, and Safari3.2.2. example code: <a href = "#" onclick = "window. open ('HTTP: // www.google.com ') "> visit Google </a> click this link to open the Google website in a new window, enter the following js code in the address bar to view the sent referrer. The Code is as follows: javascript: alert (document. referrer) test results: IE5.5 + returns an empty string Chrome3.0 +, Firefox3.5, Opera9.6, and Safari3.2.2, and returns the source webpage normally. If the source webpage is redirected under the same domain name, then we can access windoww. the opener object obtains the missing referrer information. The Code is as follows: <script type = "text/javascript"> var referrer = document. referrer; if (! Referrer) {try {if (window. opener) {// in IE, if cross-origin is used, a permission exception is thrown. // in Safari and Chrome, window. opener. location does not have any attribute referrer = window. opener. location. href ;}} catch (e) {}}</script> Cross-origin is the same ~ Mouse drag to open a new window mouse drag is now a very popular user habit, many browsers are built in or can be used by plug-ins to support the mouse drag style browsing. However, basically all pages opened in this way lose referrer. In this case, you cannot use window. opener to obtain the lost referrer. Tested: Maxthon2.5.2, Firefox FireGesture plugin, Chrome3.0 +, Opera9.6, Safari3.2. When you click the internal link of Flash and click the link on Flash to reach another website, the Referrer situation is messy. In IE, the document. the value read by the referrer is null. However, if you use the traffic monitoring software to check the value, you will find that the Referer packet header in the HTTP request actually has a value, this may be a Bug in IE implementation. At the same time, this value points to the address of the Flash file, rather than the address of the source webpage. When you click Flash in Chrome4.0 to reach a new window, Referrer is also the address of the Flash file to which it points, rather than the address of the source webpage. Chrome3.0 and Safari3.2 are the same and both will lose the Referrer information. Opera is the same as Firefox. The Referrer value is the address of the source webpage. When an HTTPS jump from an HTTPS website to an HTTP website, the browser will not send a referrer. The behavior of these browsers is the same. For example, when we use Google Reader or Gmail under HTTPS and click a link to go to another website, technically speaking, there is no difference between such access and direct Website access. The impact of Referrer loss on ad traffic monitoring if Referrer is lost, Web Analytics will lose a very important part of the information, especially for AD traffic, it is impossible to know the actual source. Currently, many websites in China that use Google Adsense advertisements use Windows. open way to open ad links, so IE will lose Referrer, and we know that IE is currently the largest browser in the market, so its impact is great. Many traffic statistics tools classify this part of traffic into "direct traffic", which is equivalent to directly typing a URL. In this case, the ad Releaser must add specific tracking parameters to the Url of the landing page when placing the ad. For example, if a Flash advertisement is clicked, what is the website address /? Src = sina. In this way, extract the src parameter using Javascript code on the landing page to obtain the ad source information. When Google Adwords is launched, the background system has an "automatic tag" option. When this option is enabled, when Google generates the landing page URLs for all ads, A gclid parameter is automatically added, which can integrate the data in the Google Analytics background and the Adwords advertisement background. In this way, you can know the ad series, AD sources, AD keywords, and other information corresponding to the ad traffic. The idea is similar to the one mentioned above. Google automatically modified the Url for you. The referer in IE is empty. If window. location. href is used in IE, the referer value is empty. The referer will not be empty if the redirection in the tag is used. The following code solves the IE problem: function gotoUrl (url) {if (window. VBArray) {var gotoLink = document. createElement ('A'); gotoLink. href = url; document. body. appendChild (gotoLink); gotoLink. click ();} else {window. location. href = url;} prohibit the browser from bringing a referer when accessing the link. When we click a link from a website to enter another page, the browser adds a Referer value to the header, to identify the source page of this access. However, this identifier may leak the user's privacy. Sometimes I don't want others to know where I clicked in. Is there any way to prevent the browser from sending Referer? • Use the newly added html5 solution, use rel = "noreferrer", and declare the connection attribute as noreferrer. Currently, only chrome4 + is supported. • The intermediate page is used, but the referrer is actually sent, for example, Google's connection redirection and noreferrer. js. • Use the javascript protocol for link transfer. See the following description. A new window is equivalent to target = "_ blank". The Code is as follows: function open_window (link) {var arg = '\ u003cscript \ u003elocation. replace ("'+ link +'") \ u003c/script \ u003e '; window. open ('javascript: window. name; ', arg) ;}</CODE> to a connection, equivalent to target = "_ self": the CODE is as follows: function redirect (link) {var arg = '\ u003cscript \ u003etop. location. replace ("'+ link +'") \ u003c/script \ u003e '; var iframe = document. createElement ('iframe'); iframe. src = 'javascript: window. name; '; iframe. name = arg; document. body. appendChild (iframe) ;}</CODE>

Related Article

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.