jQuery.browser

Contents:

jQuery.browser 官网原E文: Map

描述: 我们不推荐使用这个属性,请尝试使用功能检测来代替(见jQuery.support)。用来获取useragent的包含标志,读取 navigator.userAgent。虽然jQuery.browser不会从jQuery的未来版本被删除,竭力推荐应采用jQuery.support和适当的特征检测。

  • version added: 1.0jQuery.browser

$.browser属性允许我们检测哪一个Web浏览器正在访问网页,通过浏览器本身返回。它包含四个最流行的浏览器类(在Internet Explorer,Mozilla和Webkit,和Opera)以及每个版本信息标志。

可用的标志有:

  • webkit (as of jQuery 1.4)
  • safari (deprecated)
  • opera
  • msie
  • mozilla

此属性是即刻有效的。因此,安全地使用它来判断是否要调用$(document).ready()。在jQuery 1.3中,$.browser属性是不赞成使用的,但目前还没有计划立即将其删除。

因为$.browser使用navigator.userAgent来确定平台,它是容易被用户或浏览器本身的失实陈述欺骗。$.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

以下是一些典型的结果:

  • 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));
}
jQuery 1.6 API 中文版Clove整理、修订