Using javascript to detect browser type [duplicate]

I'm trying to use this line to detect browser type: IE or Firefox.

alert(isBrowser("Microsoft"));

but I get absolutely nothing, the alert doesn't even pop up. Not sure what I'm doing wrong.

What would be the best practice way to detect browser type?

Answers:

Answer

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)

Answer

Try this:

alert(navigator.appName);
Answer

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

Answer

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

Answer

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.
Answer

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.

Answer
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.

Answer

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 

}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.