Detecting browser using jQuery


  1. We often need to know the current running browser and its version while developing applications. 
  2. The jquery makes it easy to determine the browser infomation like browser used and its version.
  3. We use $.browser property to determine the browser details.
  4. It contains flags for each of the four most prevalent browser classes (Internet Explorer, Mozilla, Webkit, and Opera) as well as version information.

Code :

$(document).ready(function () {
    $("#browser").click(function () {
        if ($.browser.mozilla) {
            alert('Current Browser : Mozilla, Version : ' + $.browser.version);
        }

        if ($.browser.safari) {
            alert('Current Browser : Safari, Version : ' + $.browser.version);
        }

        if ($.browser.chrome) {
            alert('Current Browser : Chrome, Version : ' + $.browser.version);
        }

        if ($.browser.opera) {
            alert('Current Browser : Opera, Version : ' + $.browser.version);
        }

        if ($.browser.msie) {
                        alert('Current Browser : IE, Version : ' + $.browser.version);
        }
    });
});

Demo :




The $.browser makes use of navigator.userAgent to determine the platform.
As $.browser uses navigator.userAgent to determine the platform, it is vulnerable to spoofing by the user or misrepresentation by the browser itself. It is always best to avoid browser-specific code entirely where possible. The $.support property is available for detection of support for particular features rather than relying on $.browser.
Thus using the above jQuery code we can determine the current running browser and its version.

0 comments:

Post a Comment