21. JavaScript Browser Detection

JavaScript Browser Detection

Browser Detection
The Navigator Object

Browser Detection

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

The Navigator Object

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

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

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

to pull the version number out of the string, we are using afunction called parseFloat(),
which pulls the first thing that looks like a decimal number out of a string and returns it.

to find the version number in IE 5.0 and later, you will have to dig into a little deeper into either the appVersionor user Agent property.
the IE version will be in the form "MSIE x.x" so use a regular exression such as /MSIE \d\d.\d;/.exec(navigator.userAgent) to retutn a string containing the specified IE version

<html>
<body>
<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>

<html>
<body>
<script type="text/javascript">
document.write("<p>Browser:");
document.write(navigator.appName+"<p>");
document.write("<p>Browser Version:");
document.write(navigator.appVersion+"<p>");
document.write("<p>Code:");
document.write(navigator.appCodeName+"<p>");
document.write("<p>Platform:");
document.write(navigator.platform+"<p>");
document.write("<p>Cookies enabled:");
document.write(navigator.cookieEnabled+"<p>");
document.write("<p>Browser's user agent header:");
document.write(navigator.useragent+"<p>");
</script>
</body>
</html>

<html>
<body>
<script type="text/javascript">
var x=navigator;
document.write("codename="+x.appCodeName);
document.write("<br/>");
document.write("Minor Version="+x.appMinorVersion);
document.write("<br/>");
document.write("Name="+x.appName);
document.write("<br/>");
document.write("Version="+x.appVersion);
document.write("<br/>");
document.write("CookieEnabled="+x.cookieEnabled);
document.write("<br/>");
document.write("CPUClass="+x.cpuClass);
document.write("<br/>");
document.write("OnLine="+x.onLine);
document.write("<br/>");
document.write("Platform="+x.platform);
document.write("<br/>");
document.write("UA="+x.userAgent);
document.write("<br/>");
document.write("BrowserLanguage="+x.browserLanguage);
document.write("<br/>");
document.write("SystemLanguage="+x.systemLanguage);
document.write("<br/>");
document.write("UserLanguage="+x.userLanguage);
document.write("<br/>");
</script>
</body>
</html>


No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams