jQuery.browser

jQuery

jQuery.browser


jQuery.browser Returns: PlainObject version deprecated: 1.3, removed: 1.9

Description: Contains flags for the useragent, read from navigator.userAgent. We recommend against using this property; please try to use feature detection instead (see jQuery.support). jQuery.browser may be moved to a plugin in a future release of jQuery.

  • version added: 1.0jQuery.browser

The $.browser property provides information about the web browser that is accessing the page, as reported by the browser itself. It contains flags for each of the four most prevalent browser classes (Internet Explorer, Mozilla, Webkit, and Opera) as well as version information.

Available flags are:

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

This property is available immediately. It is therefore safe to use it to determine whether or not to call $(document).ready(). The $.browser property is deprecated in jQuery 1.3, and its functionality may be moved to a team-supported plugin in a future release of jQuery.

Because $.browser uses navigator.userAgent to determine the platform, it is vulnerable to spoofing by the user or misrepresentation by the browser itself. It is always best to avoid browser-specific code entirely where possible. The $.support property is available for detection of support for particular features rather than relying on $.browser.

Examples:

Example: Show the browser info.

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

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

1
                                  
$.browser.msie;

Example: Alerts "this is WebKit!" only for WebKit browsers

1
2
3
                                  
if ($.browser.webkit) {
alert( "this is webkit!" );
}

Example: Alerts "Do stuff for Firefox 3" only for Firefox 3 browsers.

1
2
3
4
                                  
var ua = $.browser;
if ( ua.mozilla && ua.version.slice(0,3) == "1.9" ) {
alert( "Do stuff for firefox 3" );
}

Example: Set a CSS property that's specific to a particular browser.

1
2
3
4
5
                                  
if ( $.browser.msie ) {
$("#div ul li").css( "display","inline" );
} else {
$("#div ul li").css( "display","inline-table" );
}

jQuery.browser.version Returns: String version deprecated: 1.3, removed: 1.9

Description: The version number of the rendering engine for the user's browser.

  • version added: 1.1.3jQuery.browser.version

Here are some typical results:

  • Internet Explorer: 6.0, 7.0, 8.0
  • Mozilla/Firefox/Flock/Camino: 1.7.12, 1.8.1.3, 1.9
  • Opera: 10.06, 11.01
  • Safari/Webkit: 312.8, 418.9

Note that IE8 claims to be 7 in Compatibility View.

Examples:

Example: Returns the version number of the rendering engine used by the user's current browser. For example, FireFox 4 returns 2.0 (the version of the Gecko rendering engine it utilizes).

1
2
                                  
$("p").html( "The version number of the rendering engine your browser uses is: <span>" +
$.browser.version + "</span>" );

Example: Alerts the version of IE's rendering engine that is being used:

1
2
3
                                  
if ( $.browser.msie ) {
alert( $.browser.version );
}

Example: Often you only care about the "major number," the whole number, which you can get by using JavaScript's built-in parseInt() function:

1
2
3
                                  
if ( $.browser.msie ) {
alert( parseInt($.browser.version, 10) );
}