Home > Web Front-end > JS Tutorial > Common javascript code to determine whether the version number is between version ranges_javascript skills

Common javascript code to determine whether the version number is between version ranges_javascript skills

WBOY
Release: 2016-05-16 15:29:04
Original
1531 people have browsed it

It is generally used to determine whether the version number is between the two. It can also be used to determine whether it is greater than a certain version number. If it is less than a certain version number, it can be negated

PS: It is necessary to ensure that the version specifications are consistent, for example, they are all n-digit version numbers separated by .

 var APP = {};
   //判断指定版本是否在版本范围之间,需确保版本规范一致;比如 (..,..,..)
   APP.betweenVersion = function(curr,start,end,separator){
     if(curr == start || curr == end){
       return true;
     }
     var separator = separator || '.';
     var curr = curr.split(separator);
     var start = start.split(separator);
     var end = end.split(separator);    
     var gtStart = false;
     var ltEnd = false;
 
     gtStart = APP.gtTargetVersion( APP.compareVersionEle(curr,start) );
 
     if(!gtStart){
       return false;
     }
     return !APP.gtTargetVersion( APP.compareVersionEle(curr,end) );
 
   };
   APP.compareVersionEle = function(curr,target){
     var len = curr.length;
     var temp = [];
     var left,right;
     for(var i=;i<len;i++){
 
       left = +curr[i];
       right = +target[i];
 
       if(left == right){
         temp.push();
       }else if(left > right){
         temp.push();
       }else{
         temp.push(-);
       }
     }
     return temp;
   };
   APP.gtTargetVersion = function(arr){
     var res = true;
     var curr,next;
     for(var i=,len=arr.length;i<len;i++){
       curr = arr[i];
       next = arr[i+];
       if( curr>next ){
         if(curr == ){
           res = false;
           break;
         }
         if(curr == ){
           res = true;
           break;
         }        
       }else if(curr == next){
         if(curr == -){
           res = false;
           break;
         }
         if(curr == ){
           res = true;
           break;
         }
       }else{
         if(curr == -){
           res = false;
           break;
         }
         if(curr == ){
           res = true;
           break;
         }
       }
     }
     // console.log(res);
     return res;
   }
   var res = APP.betweenVersion('...','...','...','.');
 console.log(res);
Copy after login

The current project needs to determine whether the specified version is between two versions. After searching on Baidu and Google, the codes are all limited, cannot be used universally, and even have a bunch of wrong things going around. .....

The current method has two main points. One is that the cut version number needs to be converted into a numerical array, and the other is that when comparing the size, it is converted into a comparison of three values ​​​​of -1 0 1. In this way, regardless of the separation How big is the version number? It is divided into several segments and can be compared correctly.

Let me share with you the javascript code to detect browser type and version

Object/feature detection method

This method is a general way to determine the capabilities of a browser (rather than the exact model of the browser). Most JS experts consider this approach the most appropriate because they believe scripts written this way are future-proof.

//获取IE浏览器的版本号
//返回数值,显示IE的主版本号
function getIEVer() {
 var ua = navigator.userAgent; //获取用户端信息
 var b = ua.indexOf("MSIE "); //检测特殊字符串"MSIE "的位置
 if (b < 0) {
  return 0;
 }
 return parseFloat(ua.substring(b + 5, ua.indexOf(";", b))); //截取版本号字符串,并转换为数值
}
alert(getIEVer()); //返回数值8(我的IE8)
Copy after login

Use this method if you are more concerned about the capabilities of the browser than its actual identity.

user-agent string detection method

The user-agent string provides a wealth of information about the web browser, including the browser's name and version.

var ua = navigator.userAgent.toLowerCase(); //获取用户端信息
var info = {
 ie: /msie/.test(ua) && !/opera/.test(ua),  //匹配IE浏览器
 op: /opera/.test(ua),  //匹配Opera浏览器
 sa: /version.*safari/.test(ua),  //匹配Safari浏览器
 ch: /chrome/.test(ua),  //匹配Chrome浏览器
 ff: /gecko/.test(ua) && !/webkit/.test(ua)  //匹配Firefox浏览器
};
(info.ie) && alert("IE浏览器");
(info.op) && alert("Opera浏览器");
(info.sa) && alert("Safari浏览器");
(info.ff) && alert("Firefox浏览器");
(info.ch) && alert("Chrome浏览器");
Copy after login

Usually what we do the most is to determine whether it is IE. Other browsers generally meet the standards. Some customers only need to comply with IE and FF. Then we can do this:

var isIE = (navigator.appName == "Microsoft Internet Explorer");
Copy after login

Judging IE is far more than the above method. You can use more unique things of IE, such as: window.ActiveXObject, document.all, etc. These are all object/feature detection methods! Usually they need to be used in different browsers. If you write different styles (because IE style parsing is also different), then you have to judge the version. You can do this

//获取IE浏览器的版本号
//返回数值,显示IE的主版本号
function getIEVer() {
 var ua = navigator.userAgent; //获取用户端信息
 var b = ua.indexOf("MSIE "); //检测特殊字符串"MSIE "的位置
 if (b < 0) {
  return 0;
 }
 return parseFloat(ua.substring(b + 5, ua.indexOf(";", b))); //截取版本号字符串,并转换为数值
}
alert(getIEVer()); //返回数值7
Copy after login

Detect operating system:

var isWin = (navigator.userAgent.indexOf("Win") != -1); //如果是Windows系统,则返回true
var isMac = (navigator.userAgent.indexOf("Mac") != -1);  //如果是Macintosh系统,则返回true
var isUnix = (navigator.userAgent.indexOf("X11") != -1);  //如果是Unix系统,则返回true
var isLinux = (navigator.userAgent.indexOf("Linux") != -1);  //如果是Linux系统,则返回true
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template