因为业务需求,项目经理让我做一个判断当前浏览器是否是谷歌浏览器,并确定谷歌浏览器版本,不满足谷歌浏览器和版本就显示一个页面提示升级,满足条件就跳转到我们的一个项目,下面先来实现这个:
1.当前浏览器是否是谷歌浏览器和确定浏览器浏览器版本号
JavaScript实现:
/* 检查浏览器是否为谷歌 */ (function() { //取得浏览器的userAgent字符串 var myBrowser,chromeVersion,version,userAgent = navigator.userAgent; var arr = navigator.userAgent.split(' '); for(var i=0;i < arr.length;i++){ if(/chrome/i.test(arr[i])) chromeVersion = arr[i] } if(chromeVersion){ version = Number(chromeVersion.split('/')[1].split('.')[0]); } else { version = false; } if (userAgent.indexOf("Chrome") > -1 && version >=49) { myBrowser = true } else { myBrowser = false } if(myBrowser) { // 跳转到对应的项目 } else { var wrap = document.getElementById('wrap'); wrap.style.display = "block" //让元素显示 } })();
全部的代码(图片是在网上找的谷歌的图片):
<!DOCTYPE html> <html> <head> <title>请升级您的浏览器</title> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <!-- IE8/9及以后的版本用最新的引擎渲染网页 --> <meta content="IE=edge" http-equiv="X-UA-Compatible"> <style> body{ font-size: 13px; font-family: Georgia,Verdana,sans-serif; color: #333; padding: 0; margin: 0; } .wrap{ display:none; min-width: 1024px; margin: 47px 20px; background-color: #ffffff; position: relative; } .wrap .title{ text-align: center; margin: 13px 25px; font-weight: bold; font-size: 19px; } .wrap .list{ width: 100%; margin-bottom: 10px; } .wrap .list .item{ text-align: center; padding: 10px; width: 25%; } .wrap .list .item:hover .link{ } .wrap .list .item .link{ padding-top: 110px; padding-bottom: 4px; background-position: center top; background-repeat: no-repeat; display: block; text-decoration: none; } .wrap .list .item .bc{ background-size: auto 100%; background-image: url('./images/google.jpg'); } .wrap .list .name{ width:100%; color: #e25600; text-align: center; text-decoration: underline; font-size: 19px; font-family: 'Open Sans',sans-serif; font-weight: 300; } .wrap .list .vendor{ width:100%; font-size: 10px; color: #aaa; text-align: center; display: block; margin-top: 5px; text-decoration: none; } .wrap .tag { text-align: center; margin: 13px 25px; font-size: 19px; font-family: 'Open Sans',sans-serif; font-weight: 300; } </style> </head> <body> <div class="wrap" id="wrap"> <p class="title">您的浏览器需要更新,请下载一款免费而优秀的最新版浏览器。</p> <table class="list"> <tr> <td class="item"> <a class="link bc" href="http://www.google.cn/chrome/browser/desktop/index.html" target="_blank"></a> <span class="name">Chrome</span> <span class="vendor">Google</span> </td> </tr> </table> <p class="tag">带来更多安全、极速和乐趣。</p> </div> <script> /* 检查浏览器是否为谷歌 */ (function() { //取得浏览器的userAgent字符串 var myBrowser,chromeVersion,version,userAgent = navigator.userAgent; var arr = navigator.userAgent.split(' '); for(var i=0;i < arr.length;i++){ if(/chrome/i.test(arr[i])) chromeVersion = arr[i] } if(chromeVersion){ version = Number(chromeVersion.split('/')[1].split('.')[0]); } else { version = false; } if (userAgent.indexOf("Chrome") > -1 && version >=49) { myBrowser = true } else { myBrowser = false } if(myBrowser) { // 跳转到对应的项目 } else { var wrap = document.getElementById('wrap'); wrap.style.display = "block" //让元素显示 } })(); </script> </body> </html>
效果:
2.判断当前浏览器类型
function myBrowser(){ var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 if (userAgent.indexOf("Opera") > -1) { return "Opera" }; //判断是否Opera浏览器 if (userAgent.indexOf("Firefox") > -1) { return "FF"; } //判断是否Firefox浏览器 if (userAgent.indexOf("Chrome") > -1){ return "Chrome"; } if (userAgent.indexOf("Safari") > -1) { return "Safari"; } //判断是否Safari浏览器 if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) { return "IE"; }; //判断是否IE浏览器 } //以下是调用上面的函数 var nowBrowser = myBrowser(); if ("IE" == nowBrowser) { alert("IE"); } if ("FF" == nowBrowser) { alert("Firefox"); } if ("Chrome" == nowBrowser) { alert("Chrome"); } if ("Opera" == nowBrowser) { alert("Opera"); } if ("Safari" == nowBrowser) { alert("Safari"); }
3.判断当前浏览器类型,并考虑IE5.5 6 7 8
function myBrowser(){ var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器 var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器 var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器 var isSafari = userAgent.indexOf("Safari") > -1; //判断是否Safari浏览器 if (isIE) { var IE5 = IE55 = IE6 = IE7 = IE8 = false; var verIE = new RegExp("MSIE (\\d+\\.\\d+);"); verIE.test(userAgent); var fIEVersion = parseFloat(RegExp["$1"]); IE55 = fIEVersion == 5.5; IE6 = fIEVersion == 6.0; IE7 = fIEVersion == 7.0; IE8 = fIEVersion == 8.0; if (IE55) { return "IE55"; } if (IE6) { return "IE6"; } if (IE7) { return "IE7"; } if (IE8) { return "IE8"; } }//isIE end if (isFF) { return "FF"; } if (isOpera) { return "Opera"; } } //以下是调用上面的函数 var nowBrowser = myBrowser() if (nowBrowser == "FF") { alert("Firefox"); } if (nowBrowser == "Opera") { alert("Opera"); } if (nowBrowser == "Safari") { alert("Safari"); } if (nowBrowser == "IE55") { alert("IE5.5"); } if (nowBrowser == "IE6") { alert("IE6"); } if (nowBrowser == "IE7") { alert("IE7"); } if (nowBrowser == "IE8") { alert("IE8"); }
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!