jQuery: The Write Less, Do More JavaScript Library

Utilities/jQuery.browser

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Utilities

jQuery.browser

Contains flags for the useragent, read from navigator.userAgent.
Available flags are:
  • safari
  • opera
  • msie
  • mozilla

This property is available before the DOM is ready, therefore you can use it to add ready events only for certain browsers.

There are situations where object detection is not reliable enough, in such cases it makes sense to use browser detection.

A combination of browser and object detection yields quite reliable results.

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="../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