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.
|
<script language=”javascript”> |
|
var versionBegin, versionEnd |
|
if(navigator.appName == “Netscape”) { |
|
if(navigator.userAgent.indexOf(“Firefox”) > 0) { |
|
versionBegin = navigator.userAgent.indexOf(“Firefox”) + |
|
versionEnd = navigator.userAgent.length; |
|
document.getElementById(“targetDiv”).innerHTML = |
|
navigator.userAgent.substring(versionBegin, versionEnd); |
|
if (navigator.appName == “Microsoft Internet Explorer”) { |
|
versionBegin = navigator.userAgent.indexOf(“MSIE “) + |
|
if(navigator.userAgent.indexOf(“;”, versionBegin) > 0) { |
|
versionEnd = navigator.userAgent.indexOf(“;”, versionBegin); |
|
versionEnd = navigator.userAgent.indexOf(“)”, versionBegin) |
|
document.getElementById(“targetDiv”).innerHTML = |
|
“You have Internet Explorer “ + |
|
navigator.userAgent.substring(versionBegin, versionEnd); |
|
<body onload=”checkBrowser()”> |
|
<h1>Determining your browser</h1> |
|
<div ID=”targetDiv”></div> |
</html>
By cool adch Popularity (1315 Views)