這兩天做了一個判斷瀏覽器類型和版本號碼的業務,記錄下相關的js程式碼:
function allinfo(){
var ua = navigator.userAgent;
ua = ua.toLowerCase();
var match = /(webkit) [ /]([w.] )/.exec(ua) ||
/(opera)(?:.*version)?[ /]([w.] )/.exec(ua) ||
/(msie) ([w.] )/.exec(ua) ||
!/compatible/.test(ua) && /(mozilla)(?:.*? rv:([w.] ))?/.exec(ua) || [];
//如果需要取得瀏覽器版本號:match[2]
switch(match[1]){
case "msie": //ie
if (parseInt(match[2]) === 6){ //ie6
alert("ie6");
alert("暫時不支援IE7 .0及以下版本瀏覽器,請升級您的瀏覽器版本! "show").style.display = "block";
//document.getElementById("nosee_b").style.display = "none";
}
else if (parseInt(match[2 ]) === 7) { //ie7
alert("ie7");
//document.getElementById("hid").style.display = "none";
// document. getElementById("show").style.display = "block";
}
else if (parseInt(match[2]) === 8){ //ie8
alert("ie8") ;
}
else if(parseInt(match[2]) === 9){
alert("ie9");
//document.getElementById("hid").style. display = "none";
}
break;
case "webkit": //safari or chrome
//alert("safari or chrome");
// document.getElementById ("middle").style.display = "none";
break;
case "opera": //opera
alert("opera");
break;
case " mozilla": //Firefox
alert("Firefox");
//document.getElementById("hid").style.display = "none";
break;
default:
break;
}
}