18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8<br />

Methods of Browser Detection<br />

Like most things in <strong>JavaScript</strong>, a few different forms of browser detection are available. Presently, two<br />

approaches to browser detection are used: object/feature detection and user-agent string detection. Each<br />

approach has its advantages and disadvantages, and you should understand proper usage of each when<br />

you are deploying your <strong>Web</strong> solution.<br />

Object/feature detection<br />

Object detection (also called feature detection) is a generic way of determining a browser’s capabilities<br />

rather than the exact make and model of a target browser. Most <strong>JavaScript</strong> experts point to this method<br />

as the most appropriate one to use because they believe it future proofs scripts against changes that might<br />

make it difficult to determine the exact browser being used.<br />

Object detection involves checking to see if a given object exists before using it. <strong>For</strong> instance, suppose<br />

you want to use the DOM method document.getElementById(), but you aren’t sure if the browser<br />

supports it. You can use the following code:<br />

if (document.getElementById) {<br />

//the method exists, so use it here<br />

} else {<br />

//do something else<br />

}<br />

The previous code checks whether the method exists. You have learned that a property (or method) that<br />

doesn’t exist returns a value of undefined. You may also remember that the value undefined, when<br />

translated into a Boolean, is equal to false. So, if document.getElementById() doesn’t exist, the code<br />

skips to the else clause; otherwise the first set of code is executed.<br />

Note that to check for the existence of a function, you must omit the parentheses. If<br />

you include the parentheses, the interpreter tries to call the function, which causes<br />

an error if the function doesn’t exist.<br />

This method of detection should be used when you are more concerned with the capabilities of the<br />

browser than you are with its actual identity. Throughout the book, you see examples of object detection<br />

used in specific instances, whereas in other instances another method, user-agent string detection is<br />

most appropriate.<br />

User-agent string detection<br />

User-agent string detection is the oldest browser detection method there is. Every program that accesses<br />

a <strong>Web</strong> site is required to provide a user-agent string identifying itself to the server. Traditionally, this<br />

information was only accessible from the server in the CGI environment variable HTTP_USER_AGENT<br />

(accessed by $ENV{‘HTTP_USER_AGENT’}). However, <strong>JavaScript</strong> introduced the userAgent property of<br />

the navigator object to provide client-side access to the user-agent string:<br />

var sUserAgent = navigator.userAgent;<br />

226

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!