Using javascript to detect browser type

<>

This question already has an answer here:

Answers


I hope this helps:

http://www.quirksmode.org/js/detect.html

(it's a long script, so i don't want to post it here)


Try this:

alert(navigator.appName);

I think jQuery got it right when they support testing for features instead of just browser.


For MSIE detection you may use JavaScript:

   // This function returns Internet Explorer's major version number,
   // or 0 for others. It works by finding the "MSIE " string and
   // extracting the version number following the space, up to the decimal
   // point, ignoring the minor version number
   <SCRIPT LANGUAGE="JavaSCRIPT">
   function msieversion()
   {
      var ua = window.navigator.userAgent
      var msie = ua.indexOf ( "MSIE " )

      if ( msie > 0 )      // If Internet Explorer, return version number
         return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
      else                 // If another browser, return 0
         return 0

   }
   </SCRIPT>

Below is an example of how to call it anywhere in your html:

<SCRIPT LANGUAGE="javascript">
   if ( msieversion() >= 0 )

      document.write ( "This is Internet Explorer" );

   else

      document.write ( "This is another browser" );

   </SCRIPT>

http://support.microsoft.com/kb/167820 http://support.microsoft.com/kb/167820


A very good article on this comes from Quirksmode: http://www.quirksmode.org/js/support.html

The script supplied by 'lajuette' is good but it doesn't make you much smarter. The same author explain his thinking behind the script in the above link and basically what he says is:

  • It is not about browser detection
  • It is about object detection
  • This leads to the knowledge of which browser is used.

This is basic for browser type detection but from this littel code its difficult to understand what going wrong.... Can u add body of isBrowser() that will help.


function whereUWantToDetectBrowser(){
        if (navigator.appName == "Microsoft Internet Explorer")
        intExp();
        else
        other();

}
function intExp(){
        //do what you want to do for specifically Internet Explorer
}
function other(){
        //do what you want to do for other browsers
}

this code solves the problem. Instead of calling functions from whereUWantToDetectBrowser(), if you write your specific code there, this will cause an error. And code will not run. Because a browser detects the code which it has to run (specific to each browser). and if you are distinguishing the code means the code is not working in some browsers, so you want to write it specifically for those browsers. So, other() will have not effect in IE, as intExp() has no effect in other browsers.


The best and shortest way to find the browser type for IE is.. U can do same for other browser types

if (navigator.appName == "Microsoft Internet Explorer"){

// Ur piece of validation 

}

Need Your Help

Removing URL from a URL classloader

java classloader

Is there any way to remove the URL from a URLClassloader?

ListView doesn't update until a key is pressed on the simulator

java android listview refresh

I'm working on one of my first Android apps in which I load some JSON from the web and, after parsing, display the data in a ListView. When the app first launches, the ListView is empty. After the ...