Solve the problem that the referer in js is compatible with various browsers, jsreferer

Source: Internet
Author: User

Solve the problem that the referer in js is compatible with various browsers, jsreferer

The HTTP Header referer mainly tells people where I came from, that is, to tell people which page I came from, which can be used to count the sources of users accessing the website, it can also be used for anti-leech protection. The best way to get this is Javascript. If the PHP method (for example, $ _ SERVER ['HTTP _ referer']) on the SERVER side is unreliable, it can be forged, it is difficult for people to forge the best way to use js,

Method: The document. referer method of js can be used to accurately determine the real path of a webpage. Currently, Baidu statistics, google ads statistics, and CNZZ statistics all use this method. Anti-leech is also very easy. js judges that the URLs are not shown on this site if they are not.

As we all know, our web developers hate IE, because IE does not support standards, and default behaviors outside the standards are often different from those in other browsers:

In IE, javascript is used for redirection. For example, window. location. href = "" is used. If google uses document. referrer, it cannot get the HTTP referrer requested by the browser, because IE is cleared.

Other mainstream browsers Firefox and Chrome will retain referrer, but there is no way, which means IE will enjoy the special "ministerial level:

The following code solves the ie problem:
// Check if it is an IE browser, manually add a referer
The principle is to secretly add a link to the page of IE browser, and then click the link automatically, so the referrer will be retained.

Copy codeThe Code is as follows:
Var url = 'HTTP: // www.bkjia.com ';
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;
}


Help Solve JS browser compatibility problems

From the code you provided
Var name1 = document. getElementById ('addname1'). value;
Var name2 = document. getElementById ('addname2'). value;
Var name3 = document. getElementById ('addname3'). value;
Var name4 = document. getElementById ('addname4'). value;
These rows are all incorrect.
Changed:
Var name1 = document. getElementById ('name1'). value;
......

JS compatible browsers

You need to run the program in IE8 and FF to see which statement reports an error or fails to achieve the expected purpose. Then, check the manual to see how to modify the statement in this browser, finally, your program judges the browser version, and different versions execute different programs.

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.