Handling multiple browsers - Web Designing

Unlike CGI scripts, which run on the server and don't require any particular intelligence on the part of the browser, JavaScript code is completely dependent on browser support. If you put a script on your page, browsers that don't understand JavaScript won't know what to do with it. As mentioned earlier, these browsers will interpret the code as straight text, and the result is rather unpleasant.

It's even more unpleasant, however, when your code isn't completely understood by a JavaScript-aware browser. As we've already discussed, different browsers, and different versions of the same browser, support different versions of JavaScript. A poorly written script can generate error messages or even crash a user's browser, which discourages return visits. Fortunately, JavaScript provides ways to target the browsers that understand specific JavaScript elements.

Checking for Browsers
If you have a script that you know works in Netscape 6 and IE 5.5 but doesn't work in older browsers, you may want to check browser versions and serve your script to users of the browsers in which it works, but not to users of older browsers. Using the techniques shown in this section, you can serve different scripts to different browsers. This means you can write different scripts for people using the latest browser versions and for users of the Version 4.0 browsers, for example. And you can also have an HTML-only option for browsers that don't support JavaScript (or have it turned off).

The first step is to check the browser's name and version number and assign that information to variables. The following code puts the name of the browser in a variable called browserName and the version number in a variable called browserVersion.

Depending on the name and number in these variables, the variable browser is assigned a value corresponding to the appropriate browser. Thus, if the browser is Netscape 6, browser is set to nn6; if the browser is IE 4, browser is set to ie4. After the browser identity has been assigned to this variable, you can use if/else statements to ensure that only the correct browser tries to run any browser-specific code:

In this code, the first if statement checks to see if the browser is Netscape 6, IE 5, or IE 5.5. If the user is running one of these browsers, the JavaScript code in that if statement is executed. If the browser is not Netscape 6, IE 5, or IE 5.5, the code checks for IE 4 or Navigator 4 and runs the appropriate code in either case. If the user is running an even older browser, no script is run. In any case, the body of the HTML document is displayed normally.

There are a lot of nuances to browser detection. Fortunately, there are a number of different browser detection scripts available on the Web, so you don't have to create your own.

Browser Compatibility
As I noted earlier, varying levels of JavaScript support have been built into browsers since Netscape Navigator 2.0. Table shows the version of JavaScript supported by the various versions of Internet Explorer and Netscape Navigator on different platforms.

You can use this table to plan your site's browser support and update your browser detection scripts.

Table: JavaScript support in various browsers

JavaScript support in various browsersJavaScript support in various browsers

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Web Designing Topics