SQL PHP HTML ASP JavaScript articles and free scripts to download
JavaScript Basics

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
navigator.userAgent


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
<table>
<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>";
document.write(st);
</script>
</table>
Now let us write some code to identify the browser and develop the code specific to that.
<script type="text/javascript">
var st;
st="";
if(navigator.userAgent.indexOf("Firefox")!=-1){
// 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
}
document.write(st);
</script>

Google+

Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

JavaScript Basic functions
Variables
Functions
Detecting Browser type



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer