Tuesday, January 17, 2012

How to detect browser name and version in JavaScript?

In case that you wonder how to detect version of current browser in javascript you can use the next code. If you have any question please leave a comment.
var navigatorVersion = navigator.appVersion;
var navigatorAgent = navigator.userAgent;
var browserName = navigator.appName;
var fullVersionName = '' + parseFloat(navigator.appVersion);
var majorVersionName = parseInt(navigator.appVersion, 10);
var nameOffset, verOffset, ix;

// In Firefox, the true version is after "Firefox" 
if ((verOffset = navigatorAgent.indexOf("Firefox")) != -1) {
    browserName = "Firefox";
    fullVersionName = navigatorAgent.substring(verOffset + 8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset = navigatorAgent.indexOf("MSIE")) != -1) {
    browserName = "Microsoft Internet Explorer";
    fullVersionName = navigatorAgent.substring(verOffset + 5);
}

// In Chrome, the true version is after "Chrome" 
else if ((verOffset = navigatorAgent.indexOf("Chrome")) != -1) {
    browserName = "Chrome";
    fullVersionName = navigatorAgent.substring(verOffset + 7);
}

// In Opera, the true version is after "Opera" or after "Version"
else if ((verOffset = navigatorAgent.indexOf("Opera")) != -1) {
    browserName = "Opera";
    fullVersionName = navigatorAgent.substring(verOffset + 6);
    if ((verOffset = navigatorAgent.indexOf("Version")) != -1)
        fullVersionName = navigatorAgent.substring(verOffset + 8);
}

// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset = navigatorAgent.indexOf("Safari")) != -1) {
    browserName = "Safari";
    fullVersionName = navigatorAgent.substring(verOffset + 7);
    if ((verOffset = navigatorAgent.indexOf("Version")) != -1)
        fullVersionName = navigatorAgent.substring(verOffset + 8);
}

// In most other browsers, "name/version" is at the end of userAgent 
else if ((nameOffset = navigatorAgent.lastIndexOf(' ') + 1) <
          (verOffset = navigatorAgent.lastIndexOf('/'))) {
    browserName = navigatorAgent.substring(nameOffset, verOffset);
    fullVersionName = navigatorAgent.substring(verOffset + 1);
    if (browserName.toLowerCase() == browserName.toUpperCase()) {
        browserName = navigator.appName;
    }
}
// trim the fullVersionName string at semicolon/space if present
if ((ix = fullVersionName.indexOf(";")) != -1)
    fullVersionName = fullVersionName.substring(0, ix);
if ((ix = fullVersionName.indexOf(" ")) != -1)
    fullVersionName = fullVersionName.substring(0, ix);

majorVersionName = parseInt('' + fullVersionName, 10);
if (isNaN(majorVersionName)) {
    fullVersionName = '' + parseFloat(navigator.appVersion);
    majorVersionName = parseInt(navigator.appVersion, 10);
}

//document.write(''
// + 'Browser name  = ' + browserName + '
'
// + 'Full version  = ' + fullVersionName + '
'
// + 'Major version = ' + majorVersionName + '
'
// + 'navigator.appName = ' + navigator.appName + '
'
// + 'navigator.userAgent = ' + navigator.userAgent + '
'
//)

My most popular images for sale at Shutterstock:

Find my story and how you can get into stock photography, too.

9 comments:

  1. Cred ca ar fi util sa postezi o functie misto pt detectie de device mobil. (smart phone, tablete atat Android cat si iOS)

    ReplyDelete
  2. pana acum nu am lucrat cu asa ceva :) cand va veni vremea o sa fac...desi ma gandesc ca nu e ceva foarte diferit. Daca ai tu deja, poate vrei sa ma ajuti ;;)

    ReplyDelete
  3. Eu lucrez la asa ceva momentan, dar inca mai descopar device'uri neacoperite. Cand va fi gata, o sa postez ce am scos eu :)

    ReplyDelete
  4. Try it better....

    ReplyDelete
  5. Its not Identifying for Opera browser.Is there any solution



    ReplyDelete
  6. Amateur hour much? My programming teacher would shoot me if I were to assign stuff inside of an "if".

    ReplyDelete
    Replies
    1. that seems for me very interesting :)) What do you mean? You can not assign a value value to a variable inside an if statement? How would the programming be looking with this concept? :)

      Delete