|
How to Detect the Browser Type in Javascript |
|
|
|
Contributed by Joe
|
|
Friday, 21 July 2006 |
How to Detect the Browser Type in JavascriptUsing JavaScript you can detect the type of browser the user is running. This is very useful if you want to implement features in your applications that require different code in different browsers. The key to determining the browser the user is using is the navigator object. The navigator object provides several properties you can use to tell you the type of browser being used:
- navigator.appName: This property returns a string indicating the browser that is being used. For instance, this string might be “Microsoft Internet Explorer” or “Netscape”.
- navigator.appCodeName: This property returns the browser name that the browser claims to be. For instance, in Internet Explorer 6, this will actually be “Mozilla,” as it also will be in Netscape 7.
- navigator.userAgent: This property returns the entire user agent string. The user agent string is a string sent by the browser to the server identifying itself to the server. It is from the user agent
string that the application name and the code name are derived.
Example of Detecting the Browser Type in javascript<body> <script language=”JavaScript”> document.write(“Browser Type: “ + navigator.appName + “<br>”); document.write(“Code Name: “ + navigator.appCodeName + “<br>”); document.write(“User Agent: “ + navigator.userAgent + “<br>”); </script> </body> |