11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Language Detection<br />

<strong>The</strong> final form of basic detection is to use <strong>JavaScript</strong> to sense which language the user‘s<br />

browser is set to support. We might use this to send users to a Spanish page if they have the<br />

Spanish language set as a preference in their browser. Browsers provide access to this<br />

information in slightly different ways. Netscape and Opera use the window.navigator.language<br />

property, while Internet Explorer relies on window.navigator.userLanguage or<br />

window.navigator.systemLanguage. In the case of Internet Explorer, there is some lack of<br />

clarity regarding whether we should pay attention to the operating system language or the<br />

browser language. A good guess would be to focus on the browser‘s language—and it‘s a good<br />

idea to provide links on pages to select other languages in case the detection is incorrect. <strong>The</strong><br />

following simple example illustrates the use of these properties and could easily be extended<br />

using the Location object to redirect users to language-specific pages after sensing.<br />

var lang = "en-us";<br />

if (window.navigator.language)<br />

lang = window.navigator.language<br />

else if (window.navigator.userLanguage)<br />

lang = window.navigator.userLanguage<br />

if (lang == "es")<br />

else<br />

document.write("Hola amigo!");<br />

document.write("Hi friend!");<br />

Note <strong>The</strong>re is some concern about the accuracy of the language information available in<br />

<strong>JavaScript</strong>, and some developers suggest looking at the user-agent string to see if<br />

anything is specified there as well.<br />

Advanced Detection Techniques<br />

<strong>The</strong>re are many more tricks we can use for browser detection. For example, we might be able<br />

to calculate relative download speed by delivering a set amount of data to the user and timing<br />

the transmission. We might also find it useful to add our own properties to the Navigator object<br />

to keep everything neat and organized. Microsoft also has done its part to promote improved<br />

browser detection using its client capabilities facility, which is discussed next.<br />

Microsoft Client Capabilities<br />

Microsoft introduced client capabilities detection in Internet Explorer 5 using a default behavior.<br />

We‘ll discuss behaviors in Chapter 21, but for now, take a look at the simple example here; it<br />

illustrates Explorer‘s client capabilities detection, which detects many useful properties,<br />

including connection speed.<br />

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

Saved successfully!

Ooh no, something went wrong!