How to detect my running Browser using Javascript?

Using following Javascript code, we can detect the Browsers & Browsers Version, like either running the current url is Google Chrome or Firefox or Internet Explorer or Safari.

using this way we can provide the alert to users to upgrade the old versions of browsers.


Browser Details

<script type="text/javascript">// < !&#91;CDATA&#91;
// < !&#91;CDATA&#91;
//get the launched browser details
function getLaunchBrowser() {
    var userAgent = navigator.userAgent.toLowerCase();
    //alert('userAgent'+userAgent);
    var currentBrowser = '';
	var browserCompat = 'true';
    var currentBrowserVersion = 0;
    var version = 0;
    if (userAgent.match('msie')) {
        currentBrowser = 'IE';
    }
    else if (userAgent.match('trident')) {
        currentBrowser = 'IE11';
    }
    else if (userAgent.match('chrome')) {
        currentBrowser = 'Chrome';
    }
    else if (userAgent.match('firefox')) {
        currentBrowser = 'Firefox';
    }
    else {
        currentBrowser = 'Not Supported';
    }
	//alert('currentBrowser'+currentBrowser);
    switch (currentBrowser) {
        case 'IE11':
            version = 11;
            browserCompat = "false";
            break;
        case 'IE9':
            version = 9;
			//alert('version'+version);
            browserCompat = "false";
            break;
        case 'IE':
            currentBrowserVersion = userAgent.substring(userAgent.indexOf('msie ') + 5);
            //alert('currentBrowserVersion'+currentBrowserVersion);
            version = currentBrowserVersion.split('.');
            version = version&#91;0&#93;;
			//alert('version'+version);
            if (version <= 6) {
                browserCompat = "false";
            }
            else {
            }
            break;
        case 'Chrome':
            currentBrowserVersion = userAgent.substring(userAgent.indexOf('chrome/') + 7);
            version = currentBrowserVersion.split('.');
            version = version&#91;0&#93;;
			//alert('version'+version);
            if (version <= 30) {
                browserCompat = "false";
            }
            else {
            }
            break;
        case 'Firefox':
            currentBrowserVersion = userAgent.substring(userAgent.indexOf('firefox/') + 8);
            version = currentBrowserVersion.split('.');
            version = version&#91;0&#93;;
			//alert('version'+version);
            if (version <= 25) {
                browserCompat = "false";
            }
            else {
            }
            break;
        case 'Not Supported':
            browserCompat = "false";
        default:
            break;
    }

	alert('browserCompat:::'+browserCompat);
}
// &#93;&#93;></script>

Written by 

2x Salesforce MVP | Platform Champion | 18x Salesforce Certified | MuleSoft Certified | 8x Trailhead Ranger | TechForce Services | Sydney | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*