Home > DeveloperSection > Articles > Detect Browser and Version using JavaScript

Detect Browser and Version using JavaScript


JavaScript JavaScript 
Ratings:
0 Comment(s)
 3218  View(s)
Rate this:

Detect Browser and Version using JavaScript

In this article I’m trying to explain, how to get browser name and its version (Chrome, Firefox, Internet Explorer, Opera and Safari) using JavaScript. This might be useful for web development, because some time developers need to write browser specific code.  Below I’m giving line of code that’s you can use simply copy and paste in your web project.

<script type="text/javascript">

    $(function () {

        // Get Browser Name

        var browser=navigator.appName;

        // Get All Navigator Browser Name and Version

        var b_version=navigator.appVersion;

        // Get Navigator Browser Version

        var version=parseFloat(b_version);

        // Browser Navigator Name

        var useragent=navigator.userAgent;

        switch (browser) {

            case 'Microsoft Internet Explorer':

                browser="MSIE";

                // Get Intetnet Explorer Version

                version=useragent.substr(useragent.lastIndexOf('MSIE') +5,3);

                alert(version);

                break;

            case 'Netscape':

                if (useragent.lastIndexOf('Chrome/') >0) {

                    browser="Chrome";

                    // Get Chrome Version

                    version=useragent.substr(useragent.lastIndexOf('Chrome/') +7,10);

                    alert(version);

                }

                else if (useragent.lastIndexOf('Firefox/') >0) {

                    browser="Firefox";

                    // Get Firefox Version

                    version=useragent.substr(useragent.lastIndexOf('Firefox/') +8,5);

                    alert(version);

                }

                else if (useragent.lastIndexOf('Safari/') >0) {

                    browser="Safari";

                    // Get Safari Version

                    version=useragent.substr(useragent.lastIndexOf('Safari/') +7,6);

                    alert(version);

                }

                else {

                    // Display undifine browser name

                    alert('Undefine Browser');

                }

                break;

            case 'Opera':

                version=useragent.substr(useragent.lastIndexOf('Opera/') +6,5);

                // Get Opera Version

                alert(version);

                break;

        }

    });

    </script>

 

Note:  For executing above JavaScript, Import following JavaScript file

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

 

OR

 

<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>

 

OR

 

Any “jquery.js” updated library.

 

I hope you enjoy this article and it helpful for you.


Don't want to miss updates? Please click the below button!

Follow MindStick