Getting Browser details and Version Detection using JavaScript

E-mail Print PDF

Getting Browser details and Version Detection using JavaScript

We work hard to get more traffic to make our web portal or blog popular. We use various tips and techniques to achieve our goals. We go for search engine optimization, forum and discussion boards. We put our back link there. People use it and from different parts of the world people comes to our web portal or blog. They have different platform, operating systems, and different web browsers. Some time we fail to serve page compatible to them. And pages not get properly display and next time they avoid to come back. Get your browser details here...

To avoid such situation analysis of traffic plays an important role. In this article we will go to get browser details of the web browser using JavaScript. We are going see how to get it. We can used that data to store in database and used for analyse it, so we can modify our site structure to make it more compatible with different version of web browser. Here we are not going to store it in database, we just going to display it in web browser itself. Here navigator object will play an important role. The navigator Object describes more modern ways to accomplish browser-version detection.

The navigator Object

Despite a name reminiscent of the Netscape Navigator-branded browser, the navigator object is implemented in all scriptable browsers. All browsers also implement a handful of properties that reveal the same kind of information that browsers send to servers with each page request. Thus, the navigator.userAgent property returns a string with numerous details about the browser and operating system.
For example, a script running in Internet Explorer 7 in Windows Vista receives the following value for the navigator.userAgent property:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 1.1.4322; InfoPath.2; FDM; .NET CLR 3.5.21022)
The same script running in Firefox 1.5 on a Macintosh reveals the following userAgent details:
Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13

Getting More Details

Now we will go get more details. The first thing we need to do is access the platform property of the navigator object—the value of this property will tell us which platform the user is on. navigator.appName will gives you browser name, Please see below peace of code:

<h3> Your Browser details are as follows:</h3>
<script language="javascript" type="text/javascript">
var StrDummy = navigator.appVersion;
strVersion = StrDummy.substring(0,4);
document.write("<center><table border=1 cellpadding=2><tr><td>");
document.write("<center><b>", navigator.appName,"</b>");
document.write("</td></tr><tr><td>");
document.write("<center><table border=1 cellpadding=2><tr>");
document.write("<td>Code Name: </td><td><center>");
document.write("<b>", navigator.appCodeName,"</td></tr>");
document.write("<tr><td>Version: </td><td><center>");
document.write("<b>",strVersion,"</td></tr>");
document.write("<tr><td>Platform: </td><td><center>");
document.write("<b>", navigator.platform,"</td></tr>");
document.write("<tr><td>Pages Viewed: </td><td><center>");
document.write("<b>", history.length," </td></tr>");
document.write("<tr><td>Color depth: </td><td><center>");
document.write("<b>", window.screen.colorDepth," Bits </td></tr>");
document.write("<tr><td>Java enabled: </td><td><center><b>");
if (navigator.javaEnabled()){
document.write("Yes !</td></tr>");
if(navigator.appName != "Microsoft Internet Explorer") {
vartool=java.awt.Toolkit.getDefaultToolkit();
addr=java.net.InetAddress.getLocalHost();
host=addr.getHostName();
ip=addr.getHostAddress();
document.write("<tr><td>Host Name: </td><td><center>");
document.write("<b>",host,"</td></tr>");
document.write("<tr><td>IP Address: </td><td><center>");
document.write("<b>",ip,"</td></tr>");
}
}
else document.write("No!</td></tr>")
document.write("<tr><td>Screen Resolution: </td><td><center>");
document.write("<b>",screen.width," x ",screen.height,"</td></tr>");
document.write("</table></tr></td></table></center>");
</script>

When you test code in Microsoft Internet Explorer you will get output as follows:


As some feature of Java not supported by Microsoft Internet Explorer you can’t get details like IP address and hostname. You can get it on Mozilla Firefox. I got following output we i have tested above code in Mozilla Firefox.

Source Code:

Download Source code for 'Getting Browser details and Version Detection using JavaScript '

Download

Comments (4)Add Comment
...
written by Chris Daemon, April 11, 2008
Here we are wasting our time.
USER-AGENTS are next to useless, since you can change them. I recommend analyzing the user-agent string on the server side. Using Javascript for that adds to the complexity and introduces another point of FAIL.
Conditional output/functionality depending on browser may sound intriguing, but it's the wrong approach. I would use such distinction if really necessary, say, for the XMLHttpRequest, or for embedding ActiveX vs working technology.

The Navigator object is by far not a modern way. It may be a tried way, but it's been around for years, and not part of the DOM.

If you are writing different sites for different browsers... you should google to find "Morons in Webspace"
Your Approch Worng
written by Chetankumar Akarte, April 12, 2008
Dear Chirs,
This is a simple example about, How to get browser details. Its just fun for those who want to lean JavaScript.smilies/smiley.gif

What you want to say is right. but this is just Introductory Article on navigator object supported by JavaScript.

I am not going to force user to use it real time User Tracking System. keep Commenting..!!!

Regards
Chetankumar Akarte
java 'undefined' error!!
written by newbeeJ, April 18, 2008
hi,
Thanks for the post.
I have installed IE8 beta version on my computer. when I try to run this script it gives error 'java undefined' on line

java.awt.Toolkit.getDefaultToolkit();

I'm not able to figure out the reason. Any help will be greatly appreciated

Thanks
Re: java 'undefined' error!!
written by Chetankumar Akarte, April 21, 2008
Hi newbeeJ,
"As some feature of Java not supported by Microsoft Internet Explorer you can’t get details like IP address and hostname."

So you are getting error. You have to modify if condition, as you are using IE8 beta below condition will not work properly.

if(navigator.appName != "Microsoft Internet Explorer") { }

Regards
Chetankumar Akarte

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
Last Updated ( Thursday, 10 April 2008 23:07 )  

Who's Online

We have 5 guests online

Subscribe Feeds

Enter your email address:

Sponsor Adds


Newsflash 5

Joomla! 1.5 - 'Experience the Freedom'!. It has never been easier to create your own dynamic Web site. Manage all your content from the best CMS admin interface and in virtually any language you speak.





Demo
Demo
Demo
Demo
Demo
Demo
Demo
Demo
Demo
Demo

Latest Reviews...

php|architect's Zend PHP 5 Certification Study Guide php|architect's Zend PHP 5 Certification Study Guide php|architect's Zend PHP 5 Certification Study Guide PHP is a most popular, widely used server-side scripting language ...
 
Pragmatic Unit Testing in C# with NUnit, 2nd Ed. Pragmatic Unit Testing in C# with NUnit, 2nd Ed. Pragmatic Unit Testing in C# with NUnit, 2nd Ed.While developing any .net application Unit testing is an essential techn...
 
Your Brain: The Missing Manual Your Brain: The Missing Manual Your Brain: The Missing Manual Our brain which controls our day to day activities is a mysterious creation of the Natur...
 



Apple

Apple TV

Lorem ipsum dolor sit amet consectetuer id nibh convallis suscipit netus

iTunes Gifting

iPod + iTunes Gifting

Quisque elit dolor ut congue sed ligula turpis Phasellus Aenean hac. Et Sed ipsum

Apple TV

Apple TV

Lorem ipsum dolor sit amet consectetuer id nibh convallis suscipit netus

iPod + iTunes Gifting

iPod + iTunes Gifting

Quisque elit dolor ut congue sed ligula turpis Phasellus Aenean hac. Et Sed ipsum

iPod Your Life

iPod Your Life

Tortor Aenean tincidunt pede massa Nunc id dui massa elit In. Nam amet a tellus lacus