• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 10. Using the navigator, locatio... > Detecting Browser and Version

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> 

					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint