Monday, June 2, 2014

Detecting the Browser using jQuery including IE 11

Recently  one of the team member got the issue with IE 11 while writing the code to detect the browser.The browser detection code what he has written is not working with IE 11 . The  issue here is Microsoft keep updating the User Agent String every time when new IE version released ..

To fix the issue i made few changes and now it is working .

Here is the code  to detect the browser including IE 11

<script  type="text/javascript" >
 
 function GetBrowserDetails() {
         var ua = window.navigator.userAgent
          var msie = ua.indexOf("MSIE ")
 
          // If Internet Explorer, return version number
          if (msie > 0)
          return parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)))
 
            // If Internet Explorer 11 handling differently becaue UserAgent string updated by Microsoft
           else if (!!navigator.userAgent.match(/Trident\/7\./))
               return 11;
           else
           //If another browser just returning  0
             return 0
          }
       if (GetBrowserDetails() > 0)
        alert("Hey This is Internet Explorer and the version no is " + GetBrowserDetails());
       else
         alert("Hey This is not IE browser");
 
   </script>