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.