Detecting the browser used by the visitor

There are different types of browsers with different versions used by the visitor to access a website. For a server side program there is nothing to do or special care to be taken as the code remains in severs side. However for client side tags or scripts as they gets executed at visitor end they have to give equal output for different browsers used by visitors.

The main problem is all browsers are not equal and they behave differently for different scripts. Some of the differences are kept intentionally by the browsers. So at the developer end it became difficult to develop a common code. So we ( as developer ) have to find out the visitor browser details and accordingly execute the part of the code specially written for the browser. Now how to know what is the browser being used by the visitor?

The solution is navigator object

The navigator object used to detect different properties of the browser. All properties are again not supported by all the browser but by using some common properties we can identify the browser used. We will discuss about the properties of this navigator object and the outputs we will get for different browser. Here is one sample

Here are all navigator property values of your browser ( The code is below that )

Here is the code which display above details about your browser
<script type="text/javascript">
var st;
st="<tr><td class=data><b>UserAgent </b></td><td class=data>"+ 
navigator.userAgent + "</td></tr>";
st=st + "<tr><td class=data><b>appName </b></td><td class=data>"+
 navigator.appName + "</td></tr>";
st=st + "<tr><td class=data><b>appVersion</b></td><td class=data>"+ 
navigator.appVersion + "</td></tr>";
st=st + "<tr><td class=data><b>browserLanguage</b></td><td class=data>"+ 
navigator.browserLanguage + "</td></tr>";
st=st + "<tr><td class=data><b>platform</b></td><td class=data>"+
 navigator.platform + "</td></tr>";
st=st + "<tr><td class=data><b>cookieEnabled</b></td><td class=data>"+
 navigator.cookieEnabled + "</td></tr>";
st=st + "<tr><td class=data><b>appCodeName</b></td><td class=data>"+ 
navigator.appCodeName + "</td></tr>";
Now let us write some code to identify the browser and develop the code specific to that.

<script type="text/javascript">
var st;
// Keep the code for Firefox
st=st + "Firefox";
else if (navigator.appName.indexOf("Internet Explorer")!=-1){
st=st + "Internet Explorer";
// Keep the code for Internet Explorer
else if(navigator.userAgent.indexOf("Opera")!=-1){
st=st + "Opera";
// Keep the code for Opera

Be the first to post comment on this article :


Post Comment This is for short comments only. Use the forum for more discussions.

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2015 All rights reserved worldwide Privacy Policy Disclaimer