区分浏览器,判断浏览器版本
1.用JS判斷瀏覽器是否是IE9以下,處理可能遇到的兼容性問題,或者給出瀏覽器版本過低的升級提醒。
??if(navigator.appName?==?"Microsoft?Internet?Explorer"&&parseInt(navigator.appVersion.split(";")[1].replace(/[?]/g,?"").replace("MSIE",""))<9){
?? alert("您的瀏覽器版本過低,請下載IE9及以上版本"); ??} 2.用JS判斷瀏覽器是否是IE9及以下,處理可能遇到的兼容性問題,或者給出瀏覽器版本過低的升級提醒。if(navigator.appName?==?"Microsoft?Internet?Explorer"?&& (parseInt(navigator.appVersion.split(";")[1].replace(/[?]/g,?"").replace("MSIE",""))<9) || parseInt(navigator.appVersion.split(";")[1].replace(/[?]/g,?"").replace("MSIE",""))==9)){
?? alert("您的瀏覽器版本過低,請下載IE9以上版本"); ??} 3.判斷不同瀏覽器,彈框顯示。 //定義函數(shù) function myBrowser(){??? var userAgent = navigator.userAgent; //取得瀏覽器的userAgent字符串
??? var isOpera = userAgent.indexOf("Opera") > -1;
??? if (isOpera) {
??????? 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瀏覽器
}
?
//以下是調用上面的函數(shù)
var mb = myBrowser();
if ("IE" == mb) {
??? alert("我是 IE");
}
if ("FF" == mb) {
??? alert("我是 Firefox");
}
if ("Chrome" == mb) {
??? alert("我是 Chrome");
}
if ("Opera" == mb) {
??? alert("我是 Opera");
}
if ("Safari" == mb) {
??? alert("我是 Safari");
}
??? 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 reIE = new RegExp("MSIE (\\d+\\.\\d+);");
??????? reIE.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";
??? }
}//myBrowser() end
//以下是調用上面的函數(shù)
if (myBrowser() == "FF") {
?? ? alert("我是 Firefox");
}
if (myBrowser() == "Opera") {
??? alert("我是 Opera");
}
if (myBrowser() == "Safari") {
??? alert("我是 Safari");
}
if (myBrowser() == "IE55") {
??? alert("我是 IE5.5");
}
if (myBrowser() == "IE6") {
??? alert("我是 IE6");
}
if (myBrowser() == "IE7") {
??? alert("我是 IE7");
}
if (myBrowser() == "IE8") {
?? ? alert("我是 IE8");
} 4.判斷IE瀏覽器7,8,9三個版本。 if(navigator.userAgent.indexOf("MSIE")>0){
if(navigator.userAgent.indexOf("MSIE 6.0")>0){
alert("ie6");
}
if(navigator.userAgent.indexOf("MSIE 7.0")>0){
alert("ie7");
}
//if(navigator.userAgent.indexOf("MSIE 9.0")>0 && !window.innerWidth){
if(navigator.userAgent.indexOf("MSIE 8.0")>0){ alert("ie8");
}
if(navigator.userAgent.indexOf("MSIE 9.0")>0){
alert("ie9");
}
}
輸出navigator.userAgent測試
//ie9 : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
//ie8 : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
//ie7 : Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; Tablet PC 2.0; .NET4.0E; .NET4.0C)
//Firefox: Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0
//Chrome: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
//Opera: Opera/9.80 (Windows NT 6.1; Edition IBIS) Presto/2.12.388 Version/12.14
//Safari: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2 未完待續(xù)。。。
轉載于:https://www.cnblogs.com/cassiel/p/7765916.html
總結
以上是生活随笔為你收集整理的区分浏览器,判断浏览器版本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java跨平台项目-lisa
- 下一篇: 2017年html5行业报告,云适配发布