JavaScript operations Referer detailed parsing _javascript tips

Source: Internet
Author: User
Tags http request

The importance of referrer
The HTTP request has a Referer 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.

Modifying Location objects for page navigation
The 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:

Copy Code code as follows:

Window.location.hostname = "example.com";

However, by modifying the location method of page navigation, it will result in the loss of referrer under IE.

Returns an empty string under ie5.5+

chrome3.0+,firefox3.5,opera9.6,safari3.2.2 normal return to the source page

Open a new window in window.open mode
Example:

Copy Code code as follows:

<a href= "#" onclick= "window.open (' http://www.google.com ')" > Visit 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.
Copy Code code as follows:

Javascript:alert (Document.referrer)

Test results:

Returns an empty string under ie5.5+

chrome3.0+,firefox3.5,opera9.6,safari3.2.2 normal return to 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 the lost referrer information. The code is as follows:

Copy Code code as follows:

<script type= "Text/javascript" >
var referrer = Document.referrer;
if (!referrer) {
try {
if (Window.opener) {
IE throws permission exception if cross domain
Safari and Chrome under window.opener.location without any attributes
referrer = Window.opener.location.href;
}
}
catch (e) {}
}
</script>

Cross-domain words will be out of the ~

Mouse drag to open a new window
Mouse drag and drop is now very popular user habits, many browsers are built-in or can be plug-in way 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 the Flash internal link
Click on flash to reach another site, referrer situation is more messy.

IE, the value read by 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 the bug that IE implements. 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 also points to the Flash file address, not the source page address.

Chrome3.0 and Safari3.2 are the same, are lost referrer information.

Opera is the same as Firefox, the value of referrer is the address of the source page.

HTTPS jumps to HTTP
When you jump from an HTTPS Web site to an HTTP Web site, the browser does not send 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 and the user directly type the Web site access is no difference.

The impact of referrer loss on advertising flow monitoring
Referrer if lost, the Web Analytics will lose a very 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.

In this case, the advertisers need to give the landing page URL with specific tracking parameters.

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 to come over, we can modify the landing URL for this launch, changed to Http://www.example.com/?src= Sina, like this, then extract the SRC parameter using JavaScript code in the landing page, so that 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.

The solution to the referer of IE under the null method
In IE under the window.location.href way to jump, the Referer value is empty. And in the tags inside the jump words referer will not be empty. So, you can solve this IE problem with the following code

Copy Code code as follows:

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;
}
}

prevent browsers from taking referer when accessing links
When we click on a link from one site to another, the browser adds a Referer value to the header to identify the source page for this visit. But this kind of logo may leak the user's privacy, sometimes I do not want to let others know where I clicked in, can have the means to let the browser do not send Referer?

• Using the new HTML5 solution, use 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, the equivalent of target= "_blank":

Copy Code code as follows:

function Open_window (link) {
var arg = ' \u003cscript\u003elocation.replace (' +link+ ') \u003c/script\u003e ';
window.open (' javascript:window.name; ', ARG);
}
</CODE>

Turn to a connection, equivalent to target= "_self":
Copy Code code 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>

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.