jQuery in Action

Using the jQuery flags
157
Aside from the fact that this example glosses over whatever necessarily complex
code we are using to set the flags
isIE
,
isFirefox
, and
isSafari
, we can't be sure
if these flags accurately represent the browser being used. Moreover, this code will
throw an error if used in Opera, Camino, OmniWeb, or a host of other lesser-
known browsers that might perfectly support the standard model.
Consider the following variation of this code:
if (element.attachEvent) {
element.attachEvent('onclick',someHandler);
}
else if (element.addEventListener) {
element.addEventListener('click',someHandler);
}
else {
throw new Error('event handling not supported');
}
This code doesn't perform a lot of complex, and ultimately unreliable, browser
detection; it automatically supports all browsers that support either of the two
competing event models. Much better!
Object detection is vastly superior to browser detection. It's more reliable, and
it doesn't accidentally block browsers that support the capability we are testing for
simply because we don't know about the capabilities of that browser.
NOTE
Even object detection is best avoided unless absolutely required. If we can
come up with a cross-browser solution, it should be preferred over any
type of branching.
But as superior to browser detection as object detection may be, it can't always
come to our rescue. There are times when we'll need to make browser-specific
decisions (we'll see an example in a moment) that can only be made using
browser detection.
So without any further ado, let's get around to finally answering the question...
What are the blasted browser flags?
For those times when only browser detection will do, jQuery provides a set of flags
that we can use for branching that are set up when the library is loaded, making
them available even before any ready handlers have executed. They are defined
as properties of an object instance with a reference of
$.browser
. The formal syn-
tax for this flag set is as follows:


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.