注释这段是请教了百度写的方法:
/*var version1 = "2.0.3",
version2 = "3.0.5";
version1 = version1.replace(/\./g,'');
version2 = version2.replace(/\./g,'');
console.log(version1);
console.log(version2);
console.log(version2>version1);*/
/*function compare_version(currentVersion, freshVersion){//currentVersion:当前所装的APP版本号;freshVersion:最新版本的APP的版本号
currentVersion = currentVersion.replace(/\./g,'');
currentVersion = parseInt(currentVersion);
freshVersion = freshVersion.replace(/\./g,'');
freshVersion = parseInt(freshVersion);
console.log(currentVersion);
console.log(freshVersion);
// 这里的currentVersion和freshVersion,一定要用字符串比较的方式
if(currentVersion < freshVersion){
return true;
}else{
return false;
}
}
var android = compare_version("0.9.64", "0.10.67");
var ios = compare_version("0.9.15","0.10.18");
if(android){
console.log('android:' + android+"。去下载新版本。。。");
}else{
console.log('android:' + android + "已经是最新版本");
}
if(ios){
console.log('ios:' + ios+"。去下载新版本。。。");
}else{
console.log('ios:' + ios + "已经是最新版本");
}*/
下面这段是我借鉴上面的,然后请教了iOS 10和Android工程师,他们的版本会出现的格式,下面的方法应该是把他们会出现的版本号的格式囊括进去了,欢迎各位童鞋拍砖!
/*-----------------------------所向披靡的判断版本号大小------------------------------------*/
function compare(num1, num2) {
var temp1 = parseInt(num1);
var temp2 = parseInt(num2);
if (temp1 < temp2) {
return -1;
} else if (temp1 == temp2) {
return 0;
} else {
return 1;
}
}
function compare_version(currentVersion, freshVersion) { //currentVersion:当前所装的APP版本号;freshVersion:最新版本的APP的版本号
var currentVersionArray = currentVersion.split('.');
var freshVersionArray = freshVersion.split('.');
console.log('currentVersionArray: ' + currentVersionArray);
console.log('freshVersionArray: ' + freshVersionArray);
var maxLen = currentVersionArray.length;
if (currentVersionArray.length < freshVersionArray.length) {
maxLen = freshVersionArray.length;
var which = freshVersionArray.length - currentVersionArray.length;
if (which > 0) {
for (var i = which; i > 0; i--) {
currentVersionArray.push("0");
}
}
} else if (currentVersionArray.length > freshVersionArray.length) {
maxLen = currentVersionArray.length;
var which = currentVersionArray.length - freshVersionArray.length;
if (which > 0) {
for (var i = which; i > 0; i--) {
currentVersionArray.push("0");
}
}
}
for (var i = 0; i < maxLen; i++) {
var temp1 = compare(parseInt(currentVersionArray[i]), parseInt(freshVersionArray[i]));
if (temp1 == 0) {
if (i == currentVersionArray.length - 1) {
return true;
}
} else if (temp1 == 1) {
return true;
} else if (temp1 == -1) {
return false;
}
}
}
/*------------用法(测试)---------------*/
console.log('这是Android的版本比较-------------------------------------');
var android = compare_version("1.32", "1.32");
console.log('这是ios的版本比较-------------------------------------');
var ios = compare_version("0.10.18", "0.10.18");
if (!android) {
console.log('android:' + android + "。去下载新版本。。。");
} else {
console.log('android:' + android + "已经是最新版本");
}
if (!ios) {
console.log('ios:' + ios + "。去下载新版本。。。");
} else {
console.log('ios:' + ios + "已经是最新版本");
}