O'Reilly logo

jQuery Pocket Reference by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. Utility Functions

The jQuery library defines a number of utility functions (as well as two properties) that you may find useful in your programs. As you’ll see in the list below, a number of these functions now have equivalents in ECMAScript 5 (ES5). jQuery’s functions predate ES5 and work in all browsers. In alphabetical order, the utility functions are:

jQuery.browser

The browser property is not a function, but an object that you can use for client sniffing or browser testing. This object will have the property msie set to true if the browser is IE. The mozilla property will be true if the browser is Firefox or related. The webkit property will be true for Safari and Chrome, and the opera property will be true for Opera. In addition to this browser-specific property, the version property contains the browser version number. Client sniffing is best avoided whenever possible, but you can use this property to work around browser-specific bugs with code like this:

if ($.browser.mozilla &&
    parseInt($.browser.version) < 4) {
    // Work around a hypothetical Firefox bug here.
}
jQuery.contains()

This function expects two document elements as its arguments. It returns true if the first element contains the second element, and returns false otherwise.

jQuery.each()

Unlike the each() method, which iterates only over jQuery objects, the jQuery.each() utility function iterates through the elements of an array or the properties of an object. The first argument is the array or object to be ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required