ドキュメント

Utilities/jQuery.browser

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Utilities

jQuery.browser

We recommend against using this property, please try to use feature detection instead (see jQuery.support). Contains flags for the useragent, read from navigator.userAgent. While jQuery.browser will not be removed from future versions of jQuery, every effort to use jQuery.support and proper feature detection should be made.
Available flags are:
  • safari
  • opera
  • msie
  • mozilla
Examples:

Show the browser info.

    jQuery.each(jQuery.browser, function(i, val) {
      $("<div>" + i + " : <span>" + val + "</span>")
                .appendTo(document.body);
    });

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    jQuery.each(jQuery.browser, function(i, val) {
      $("<div>" + i + " : <span>" + val + "</span>")
                .appendTo(document.body);
    });

  });
  </script>
  <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>
</head>
<body>
  <p>Browser info:</p>
</body>
</html>

Returns true if the current useragent is some version of Microsoft's Internet Explorer.

$.browser.msie

Alerts "this is safari!" only for safari browsers

if ($.browser.safari) {
   alert("this is safari!");
}

NameType
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")
});

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");
  }
});