jQuery.browser
jQuery.browser 返回: Map
描述: 我們不推薦使用這個屬性,請嘗試使用功能檢測來代替(見jQuery.support)。用來獲取useragent的包含標(biāo)志,讀取 navigator.userAgent。雖然jQuery.browser不會從jQuery的未來版本被刪除,竭力推薦應(yīng)采用jQuery.support和適當(dāng)?shù)奶卣鳈z測。
version added: 1.0jQuery.browser
$.browser屬性允許我們檢測哪一個Web瀏覽器正在訪問網(wǎng)頁,通過瀏覽器本身返回。它包含四個最流行的瀏覽器類(在Internet Explorer,Mozilla和Webkit,和Opera)以及每個版本信息標(biāo)志。
可用的標(biāo)志有:
- webkit (as of jQuery 1.4)
- safari (deprecated)
- opera
- msie
- mozilla
此屬性是即刻有效的。因此,安全地使用它來判斷是否要調(diào)用$(document).ready()。在jQuery 1.3中,$.browser屬性是不贊成使用的,但目前還沒有計劃立即將其刪除。
因?yàn)?code>$.browser使用navigator.userAgent來確定平臺,它是容易被用戶或?yàn)g覽器本身的失實(shí)陳述欺騙。$.support屬性比$.browser提供更有效的檢測特定功能的支持。
Examples:
Example: Show the browser info.
<!DOCTYPE html>
<html>
<head>
<style>
p { color:green; font-weight:bolder; margin:3px 0 0 10px; }
div { color:blue; margin-left:20px; font-size:14px; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>Browser info:</p>
<script>
jQuery.each(jQuery.browser, function(i, val) {
$("<div>" + i + " : <span>" + val + "</span>")
.appendTo(document.body);
});</script>
</body>
</html>
Demo:
Example: Returns true if the current useragent is some version of Microsoft's Internet Explorer.
$.browser.msie
Example: Alerts "this is webkit!" only for webkit browsers
if ($.browser.webkit) {
alert("this is webkit!");
}
Example: Alerts "Do stuff for firefox 3" only for firefox 3 browsers.
jQuery.each(jQuery.browser, function(i, val) {
if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.9")
alert("Do stuff for firefox 3")
});
Example: Set a CSS property to specific browser.
jQuery.each(jQuery.browser, function(i) {
if($.browser.msie){
$("#div ul li").css("display","inline");
}else{
$("#div ul li").css("display","inline-table");
}
});
jQuery.browser.version Returns: String
Description: 用戶的瀏覽器渲染引擎的版本號。
version added: 1.1.3jQuery.browser.version
以下是一些典型的結(jié)果:
- Internet Explorer: 6.0, 7.0
- Mozilla/Firefox/Flock/Camino: 1.7.12, 1.8.1.3, 1.9
- Opera: 9.20
- Safari/Webkit: 312.8, 418.9
請注意,IE8兼容性視要求成為ie 7。 Note that IE8 claims to be 7 in Compatibility View.
Examples:
Example: Returns the browser version.
<!DOCTYPE html>
<html>
<head>
<style>
p { color:blue; margin:20px; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>
</p>
<script>
$("p").html("The browser version is: <span>" +
jQuery.browser.version + "</span>");
</script>
</body>
</html>
Demo:
Example: Alerts the version of IE that is being used
if ( $.browser.msie ) {
alert( $.browser.version );
}
Example: Often you only care about the "major number," the whole number. This can be accomplished with JavaScript's built-in parseInt() function:
if (jQuery.browser.msie) {
alert(parseInt(jQuery.browser.version));
}