Breadcrumbs

Checking the Browser

JavaScript can be useful in some hard spots, and a browser detecting script can be very convenient. Detecting the browser of the visitor can easily assist in cross browser web design. JavaScript allows you to access the user's browser via the navigator object:

Code: JavaScript

<script type="text/javascript">
    var browser = navigator.appName;
    alert(browser);
</script>

You can also use another property of the navigator object known as userAgent to access a description of the browser version of the appName property. Here we can combine the two to make a cleaner alert message:

Code: JavaScript

<script type="text/javascript">
    var browser = navigator.appName;
    var description = navigator.userAgent;
    alert(browser + &#8220;/n&#8221; + description);
</script>

Some web browsers lack the navigator object, although the latest releases of the major web browsers support it. So when your stuck between a rock and a hard spot, just think JavaScript!

Comments

  • Well if you are already writing in a scripting language, like PHP, you can use the built in features.

    Posted by Frank0051 (external link) on Sun 24 Jul 2005 at 17:44

  • PHP has no ability to determine what the browser is, or at least as far as I know.

    Posted by Ethan Poole (external link) on Mon 25 Jul 2005 at 16:48

  • And if it did, it would most likely be old-fashioned the day after... razz

    You can use the user agent string sent, of course..

    Posted by Frans (external link) on Tue 26 Jul 2005 at 7:15