in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 34 35 36 37 38 39 < 40 > 41 42 43 44 45 46 .. 118 >> Next

Best of all, you see real working examples of all the techniques presented in Part II. (The examples are also included on the CD-ROM at the back of this book, so you don’t even have to type the code.)
Chapter 5
Detecting Your Users' Browser Environments
ln This Chapter
► Understanding how (and why) JavaScript support differs among browsers
► Applying strategies for cross-platform script creation
► Taking advantage of advanced JavaScript features with a browser-detection script
7he biggest challenge facing Web developers today isn’t hardware- or software-based: It’s wetware-based. (Wetware — a term that refers to the supposed squishiness of the human brain — is geek-speak for human beings.) And that challenge is trying to get the companies that create Web browsers to agree on a single, standard implementation of browser-supported technologies like JavaScript!
With the current situation, the brand of browser that someone has installed, the browser’s version, and the underlying operating system all affect that person’s ability to view your JavaScript-enabled Web pages. As a JavaScript developer, you need to be aware of the differences in JavaScript implementations among browsers and write your scripts accordingly. If you don’t, you might end up creating a whiz-bang script that runs only on your computer.
Whacking Your Way through the Browser Maze
From the latest reports, both Microsoft and Netscape have promised to support the ECMAScript standard (which I discuss in detail in Chapter 3) in future versions of their respective browsers.
106 Part II: Creating Dynamic Web Pages
Even if Internet Explorer and Netscape Navigator were fully ECMAScript-compliant (and offered no additional features), the same JavaScript script still might not execute identically in both browsers. Why? For JavaScript to be a true cross-browser language, both the syntax and the document object model (DOM) would have to be consistent.
ECMA-262 takes JavaScript halfway to cross-browser nirvana by defining a standard language specification, but it doesn’t define the DOM. As you see in Chapter 4, the DOMs for the two browsers are far from identical, despite the efforts of the World Wide Web Consortium to define a unified standard.
Fortunately, as you see in the next section, you don’t have to depend on differences between JavaScript implementation and object models to write great cross-browser scripts. All you need to do is identify the differences at runtime and display customized Web pages accordingly.
Detecting Features
By using JavaScript, you can detect what make and version of Web browser a user is using to view your pages — useful information that lets you customize Web pages on-the-fly to provide your users with the best possible viewing experience. But make and version aren’t the only bits of browser-related information that you can detect by using JavaScript. You can also determine which Java applets and browser plug-ins a user has installed, which Web page your user visited directly before surfing to yours (called the referring page), and even user preferences. Read on to find out how!
Browser make and version
The most reliable way to figure out which browsers are loading your script is to ask. You ask programmatically, using JavaScript, by adding a bit of code to the beginning of your script, querying the DOM for browser-specific details. When you determine which make, model, and version of browser is attempting to load your JavaScript-enabled Web page, you can display your page accordingly.
The easiest way to implement this functionality is to use the <MARQUEE> tag, which is an HTML tag (and corresponding scripting object) supported by Internet Explorer (beginning with version 3.x). The trouble is that some versions of Navigator don’t support the <MARQUEE> tag. When a non-marquee-supporting browser loads a Web page containing the <MARQUEE> tag, it might do one of three things:
Chapter 5: Detecting Your Users' Browser Environments 107
Display the scrolling text statically or not at all Ignore your marquee-related JavaScript code Generate a JavaScript error
One way to ensure that your viewers see what you want them to see is to use JavaScript to see whether the browser loading your script is Internet Explorer.
If it is, you can use the <MARQUEE> tag with confidence.
If the browser isn’t Internet Explorer, you can display the scrolled information in an alternate eye-catching fashion — for example, as a bolded, centered heading.
Listing 5-1 shows the code for a “sniffer” script that examines (sniffs out) browser settings and displays a string of text as either a scrolling marquee or as a bolded, centered heading, depending on whether the browser loading the script is Internet Explorer.
A custom fit, every time
Creating different versions of each of your Web pages for each and every different browser version in existence ensures an optimum experience for all of your users. It also represents a maintenance nightmare!
Previous << 1 .. 34 35 36 37 38 39 < 40 > 41 42 43 44 45 46 .. 118 >> Next