Code _javascript tips to determine the JavaScript version of a browser

Source: Internet
Author: User
The old man said that the code to be executed should be placed under nesting as shown in < script language= "JavaScript1.2" >. But when asked to detect JavaScript versions, get the following code:
Copy Code code as follows:

var js_ver = [];
(Number.prototype.toFixed)? Js_ver.push ("1.5"): false;
([].indexof && [].foreach]? Js_ver.push ("1.6"): false;
((function () {try {[a,b] = [0,1];return true;} catch (ex) {return false;}}) ())? Js_ver.push ("1.7"): false;
([].reduce && [].reduceright && JSON]? Js_ver.push ("1.8"): false;
("". Trimleft)? Js_ver.push ("1.8.1"): false;
Js_ver.supports = function ()
{
if (Arguments[0])
Return (!!!) ~this.join (). IndexOf (Arguments[0] + ",") + ",");
Else
Return (This[this.length-1]);
}
Alert ("Latest Javascript version supported:" + js_ver.supports ());
Alert ("Support for version 1.7:" + js_ver.supports ("1.7"));

This script detects JavaScript versions by detecting features, and checks for features supported by specific JavaScript versions.
With the results, let's take a look at how this system detects JavaScript versions, then extract the code from it (the extraction process is rather tangled) and get the following code:
Copy Code code as follows:

<script type= "Text/javascript" >
var n = navigator;
var u = n.useragent;
var apn = N.appname;
var v = n.appversion;
var ie = v.indexof (' MSIE ')
if (ie > 0) {
APV = parseint (i = v.substring (ie + 5));
if (APV > 3) {
APV = parsefloat (i);
}
}else{
APV = parsefloat (v);
}
var Isie = (APN = = ' Microsoft Internet Explorer ');
var Ismac = (u.indexof (' Mac ') >= 0);
var javascriptversion = "1.0";
if (String && string.prototype) {
Javascriptversion = ' 1.1 ';
if (Javascriptversion.match) {
javascriptversion = ' 1.2 ';
var tm = new Date;
if (tm.setutcdate) {
Javascriptversion = ' 1.3 ';
if (Isie && ismac && APV >= 5) javascriptversion = ' 1.4 ';
var pn = 0;
if (pn.toprecision) {
javascriptversion = ' 1.5 ';
A = new Array;
if (A.foreach) {
Javascriptversion = ' 1.6 ';
i = 0;
o = new Object;
TCF = new Function (' O ', ' var e,i=0;try{i=new iterator (O)}catch (e) {}return i ');
i = TCF (o);
if (i && i.next) {
javascriptversion = ' 1.7 ';
}
}
}
}
}
}
alert (javascriptversion);
</script>

Code implementation principle: Depending on the different version of JavaScript for some specific function support different to determine its version. Only one special treatment is available for version 1.4.
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.