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 ><b>UserAgent </b></td><td >"+ 
navigator.userAgent + "</td></tr>";
st=st + "<tr><td ><b>appName </b></td><td >"+
 navigator.appName + "</td></tr>";
st=st + "<tr><td ><b>appVersion</b></td><td >"+ 
navigator.appVersion + "</td></tr>";
st=st + "<tr><td ><b>browserLanguage</b></td><td >"+ 
navigator.browserLanguage + "</td></tr>";
st=st + "<tr><td ><b>platform</b></td><td >"+
 navigator.platform + "</td></tr>";
st=st + "<tr><td ><b>cookieEnabled</b></td><td >"+
 navigator.cookieEnabled + "</td></tr>";
st=st + "<tr><td ><b>appCodeName</b></td><td >"+ 
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>

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer