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.

Browser and Operating System Detection<br />

The user-agent string provides a lot of information about a <strong>Web</strong> browser, including the browser name<br />

and version. This is why <strong>Web</strong> site traffic evaluation software uses the user-agent string to determine how<br />

many of your visitors are using a particular browser or operating system. The following table displays<br />

some common browsers and their user-agent strings<br />

Browser<br />

User-Agent String<br />

Internet Explorer 6.0 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)<br />

(Windows XP)<br />

Mozilla 1.5<br />

(Windows XP)<br />

Firefox 0.92<br />

(Windows XP)<br />

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5)<br />

Gecko/20031007<br />

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7)<br />

Gecko/20040707 Firefox/0.8<br />

Opera 7.54 Opera/7.54 (Windows NT 5.1; U)<br />

(Windows XP)<br />

Safari 1.25<br />

(MacOS X)<br />

Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)<br />

Apple-<strong>Web</strong>Kit/124 (KHTML, like Gecko) Safari/125.1<br />

Just a quick look at these user-agent strings reveals a lot about the browsers that are generating them.<br />

Also, you may notice just how different each browser’s user-agent string is. Opera’s user-agent string is<br />

pretty short whereas Safari’s is extremely long. You may also notice that Internet Explorer’s user-agent<br />

string looks suspiciously like Mozilla’s, and Safari’s says like Gecko. The history of how user-agent<br />

strings developed is a very revealing journey into how browsers have developed over the years.<br />

A (Not So) Brief History<br />

of the User-Agent String<br />

Before you delve into user-agent detection, you should understand why the detection script looks for certain<br />

parts of a user-agent string. Understanding user-agent strings can be very difficult without understanding<br />

why and how they developed. This section takes a look into the evolution of user-agent strings<br />

from early browser, such as Netscape Navigator 3.0, through modern-day browsers, such as Safari.<br />

Netscape Navigator 3.0 and Internet Explorer 3.0<br />

The browser that spearheaded the popularity of the <strong>Web</strong> was Netscape Navigator 3.0, which was<br />

released around 1996. The code name of the Netscape engine was Mozilla, and the user-agent string had<br />

a very simple format:<br />

Mozilla/AppVersion (Platform; Security [; OS-or-CPU-Description])<br />

<strong>For</strong> example, Netscape Navigator 3.0 running on Windows 95 would have the following user-agent string:<br />

Mozilla/3.0 (Win95; I)<br />

227

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

Saved successfully!

Ooh no, something went wrong!