判断浏览器的版本,出现低版本提示
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var BrowserVision = { 2 Get: function () { 3 /// <summary> 4 /// 获取浏览器名称及版本 5 /// </summary> 6 /// <returns type="String"/> 7 var agent = navigator.userAgent; 8 var browser = ""; 9 var version = ""; 10 if (/MSIE/.test(agent)) { 11 browser = "IE"; 12 var reg_match = agent.match(/MSIE[\w\W]+;/)[0]; 13 reg_match = reg_match.split(";")[0]; 14 version = reg_match.split(" ")[1]; 15 } 16 else if (/Trident/.test(agent) && /rv:11.0/.test(agent)) { 17 browser = "IE"; 18 version = "11.0" 19 } 20 else if (/Safari/.test(agent) && !(/Chrome/.test(agent)) && !(/Edge/.test(agent))) { 21 var reg_match = agent.match(/Safari\/[\w\W]+$/)[0]; 22 browser = "Safari"; 23 version = reg_match.split("/")[1]; 24 } 25 else if (/Safari/.test(agent) && /Chrome/.test(agent) && !(/Edge/.test(agent))) { 26 var reg_match = agent.match(/Chrome\/[\w\W]+\s/)[0]; 27 browser = "Chrome"; 28 version = reg_match.split("/")[1]; 29 } 30 else if (/Safari/.test(agent) && /Edge/.test(agent)) { 31 var reg_match = agent.match(/Edge\/[\w\W]+$/)[0]; 32 browser = "Edge"; 33 version = reg_match.split("/")[1]; 34 } 35 else { 36 browser = "Unknow"; 37 version = "0"; 38 } 39 return browser + "," + version; 40 }, 41 IsIE: function () { 42 /// <summary> 43 /// 判定是否为IE 44 /// --是返回1,否则返回0 45 /// </summary> 46 /// <returns type="int"/> 47 var agent = navigator.userAgent; 48 if (/MSIE/.test(agent) || (/Trident/.test(agent) && /rv:11.0/.test(agent))) { 49 return 1; 50 } 51 else { 52 return 0; 53 } 54 }, 55 IELowerThan: function (version) { 56 /// <summary> 57 /// 判定IE是否小于指定版本 58 /// --是返回1 59 /// --不小于指定版本返回0;不是IE返回0;错误的参数返回0 60 /// </summary> 61 /// <param type="float" name="version"> 62 /// 版本号 63 /// </param> 64 /// <returns type="int"/> 65 if (version != undefined && !isNaN(version)) { 66 var browser = BrowserVision.Get(); 67 if (/IE/.test(browser)) { 68 var brow_ver = parseFloat(browser.split(",")[1]); 69 var version = parseFloat(version); 70 if (brow_ver < version) { 71 return 1; 72 } 73 else { 74 return 0; 75 } 76 } 77 else { 78 return 0; 79 } 80 } 81 else { 82 return 0; 83 } 84 } 85 }
以上是一些了的浏览器版本判断的逻辑,然后我们只需要调用一下 if (BrowserVision.IELowerThan(8)) {
低版本浏览器的操作。我是用了两个兄弟级div包含正常内容和提示内容,通过div的显隐达到效果
// $(".js-content").hide();
// $(".js-update").show();
}