W3Schools

home HOME

JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Variables
JS If...Else
JS Switch
JS Operators
JS Popup Boxes
JS Functions
JS For Loop
JS While Loop
JS Break Loops
JS For...In
JS Events
JS Try...Catch
JS Throw
JS onerror
JS Special Text
JS Guidelines

JS Objects
JS Objects Intro
JS String
JS Date
JS Array
JS Boolean
JS Math
JS HTML DOM

JS Advanced
JS Browser
JS Cookies
JS Validation
JS Animation
JS Image Maps
JS Timing
JS Create Object
JS Summary

Examples/Quiz
JS Examples
JS Object Examples
JS DOM Examples
JS Quiz

JS References
JS Objects
JS HTML DOM

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Forum

Helping W3Schools

pixels

JavaScript Browser Detection

previous next

The JavaScript Navigator object contains information about the visitor's browser.


Examples

Detect the visitor's browser and browser version

More details about the visitor's browser

All details about the visitor's browser

Alert user, depending on browser


Browser Detection

Almost everything in this tutorial works on all JavaScript-enabled browsers. However, there are some things that just don't work on certain browsers - specially on older browsers.

So, sometimes it can be very useful to detect the visitor's browser type and version, and then serve up the appropriate information.

The best way to do this is to make your web pages smart enough to look one way to some browsers and another way to other browsers.

JavaScript includes an object called the Navigator object, that can be used for this purpose.

The Navigator object contains information about the visitor's browser name, browser version, and more.


The Navigator Object

The JavaScript Navigator object contains all information about the visitor's browser. We are going to look at two properties of the Navigator object:

  • appName -  holds the name of the browser
  • appVersion - holds, among other things, the version of the browser

Example

<html>
<body>
<script type="text/javascript">
var browser=navigator.appName
var b_version=navigator.appVersion
var version=parseFloat(b_version)
document.write("Browser name: "+ browser)
document.write("<br />")
document.write("Browser version: "+ version)
</script>
</body>
</html>

The variable browser in the example above holds the name of the browser, i.e. "Netscape" or "Microsoft Internet Explorer".

The appVersion property in the example above returns a string that contains much more information than just the version number, but for now we are only interested in the version number. To pull the version number out of the string we are using a function called parseFloat(), which pulls the first thing that looks like a decimal number out of a string and returns it.

IMPORTANT! The version number is WRONG in IE 5.0 or later! Microsoft starts the appVersion string with the number 4.0. in IE 5.0 and IE 6.0!!! Why did they do that??? However, JavaScript is the same in IE6, IE5 and IE4, so for most scripts it is ok.

Example

The script below displays a different alert, depending on the visitor's browser:

<html>
<head>
<script type="text/javascript">
function detectBrowser()
{
var browser=navigator.appName var b_version=navigator.appVersion var version=parseFloat(b_version) if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version>=4))
{alert("Your browser is good enough!")} else {alert("It's time to upgrade your browser!")}
} </script> </head>
<body onload="detectBrowser()">
</body>
</html>


previous next


Learn XML with Stylus Studio XML Tools - Free Download!

Stylus Studio - The World's Best XML IDE   

Stylus Studio makes it easy to edit and validate XML, XSLT, DTD, XML Schema, XHTML, XPath, XQuery and Web Service applications.

Free XML video demonstrations will help you learn XML and become a W3C XML development master in just minutes!

Download a FREE Trial Now!



Jump to: Top of Page or HOME or Printer Friendly Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its contents. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2007 by Refsnes Data. All Rights Reserved.

Validate Validate W3C-WAI level A conformance icon W3Schools was converted to XHTML in December 1999
SITE SEARCH
 
About W3Schools
W3Schools Forum
E Components
Ecommerce
Components
for ASP.NET
WEB HOSTING
$15 Domain Name
Registration
Save $20 / year!
UK Domain Names
UK Web Hosting

Alojamiento Web
Buy UK Domain Names
Register Domain Names
Cheap Domain Names
Cheap Web Hosting
Best Web Hosting
Bulk Domain Names
UK Web Hosting
PHP MySQL Hosting
Web Hosting UK
Top 10 Web Hosting
Web Hosting Providers
ASP.NET Web Hosting
3 Months Free!
Programming Forums
Windows Web Hosting
WEB BUILDING
XML Website Tools
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Custom Programming
BEST Flash Templates
Web Design Forum
Programming Forum
FREE Trial or Demo
Web Content Manager
Forms,Web Alerts,RSS
HTML Exam
SHOPPING
UK Wholesalers
UK Wholesale
ONLINE DEGREE
Advance Your Career
with Drexel University
Bachelor's & Master's
Tech Degrees ONLINE

pixels