Determining browser type and version

Here’s a more advanced example that determines which browser the user has and lets you execute code depending on browser type to display the browser version. This example puts to use the if and else statements as well as several built-in JavaScript functions that handle strings. In JavaScript, text strings are considered objects, and they have some built-in properties and methods that make life easier.

<html>

<head>

<title>

Determining your browser

</title>

<script language=”javascript”>

var versionBegin, versionEnd

function checkBrowser()

{

if(navigator.appName == “Netscape”) {

if(navigator.userAgent.indexOf(“Firefox”) > 0) {

versionBegin = navigator.userAgent.indexOf(“Firefox”) +

“Firefox”.length + 1;

versionEnd = navigator.userAgent.length;

document.getElementById(“targetDiv”).innerHTML =

“You have Firefox “ +

navigator.userAgent.substring(versionBegin, versionEnd);

}

}

if (navigator.appName == “Microsoft Internet Explorer”) {

versionBegin = navigator.userAgent.indexOf(“MSIE “) +

“MSIE “.length;

if(navigator.userAgent.indexOf(“;”, versionBegin) > 0) {

versionEnd = navigator.userAgent.indexOf(“;”, versionBegin);

} else {

versionEnd = navigator.userAgent.indexOf(“)”, versionBegin)

+ 2;

}

document.getElementById(“targetDiv”).innerHTML =

“You have Internet Explorer “ +

navigator.userAgent.substring(versionBegin, versionEnd);

}

}

</script>

</head>

<body onload=”checkBrowser()”>

<h1>Determining your browser</h1>

<div ID=”targetDiv”></div>

</body>

</html>

By cool adch   Popularity  (1296 Views)