Detecting Browser and Version


Because the format of the userAgent property varies so much, it can be hard to know how to extract the browser's version from it. Here's a script we originally saw in Chapter 4, "Handling the Browser Environment." This script was designed to solve that problem, filling a variable named version with the floating-point version of the browser. It's designed to work with all scriptable versions of the two browsers, from version 3.0 to 6.0 of the Internet Explorer and from version 2.0 to 6.2 of the Netscape Navigator, but there are no guarantees . Some odd versions or versions for little-supported operating systems of each browser may use a different format for the userAgent property. Here's the code:

(Listing 10-01.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Getting Browser Version          </TITLE>      </HEAD>      <BODY>          <H1>Getting Browser Version</H1>          <SCRIPT LANGUAGE="JavaScript">              <!--  var start, end, version   if(navigator.appName == "Netscape") {   if(navigator.userAgent.indexOf("Netscape") < 0) {   start = "Mozilla/".length   end = navigator.userAgent.indexOf(" ", start)   version = parseFloat(navigator.userAgent.substring(start, end))   document.write("You are using Netscape Navigator " + version)   } else {   start = navigator.userAgent.indexOf("Netscape") +   "Netscape".length + 2   end = navigator.userAgent.length   version = parseFloat(navigator.userAgent.substring(start, end))   document.write("You are using Netscape Navigator " + version)   }   }   if (navigator.appName == "Microsoft Internet Explorer") {   start = navigator.userAgent.indexOf("MSIE ") + "MSIE ".length   if(navigator.userAgent.indexOf(";", start) > 0) {   end = navigator.userAgent.indexOf(";", start)   } else {   end = navigator.userAgent.indexOf(")", start)   }   version = parseFloat(navigator.userAgent.substring(start, end))   document.write("You are using Internet Explorer " + version)   }  // -->          </SCRIPT>      </BODY>  </HTML> 

You can see the results in the Netscape Navigator in Figure 10.1. In this book, I list the browser support by version of each JavaScript statement, property, method, and event, and using a script something like this one, you can check version information before attempting to put those items to work.

Figure 10.1. Getting browser version.

graphics/10fig01.gif



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net