Getting Browser details and Version Detection using JavaScript
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