Browser Detection with Javascript: IE, FF, Opera

Browser Detection with Javascript: IE, FF, Opera

 

Last night i was working with a JavaScript program. For this reason i have to check what is my current browser for which i have to run a separate JavaScript function. But i was try couple of code snippet from web but none of the working exactly what i want. i have to the browser with their current version. Hence i found a code snippet at JavaScript kit. Really this is what exactly one. but it just gives a example for Firefox, Internet Explorer, Opera.

You can get the details at JavaScript kit. Here is the code snippet.

Firefox Browser Detection

   1: //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
   2: if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
   3: { 
   4:     // capture x.x portion and store as a number
   5:     var ffversion = new Number(RegExp.$1) 
   6:     
   7:     if (ffversion >= 3)
   8:     {
   9:         document.write("You're using FF 3.x or above");
  10:     }
  11:     else if (ffversion >= 2)
  12:     {
  13:         document.write("You're using FF 2.x");
  14:     }
  15:     else if (ffversion >= 1)
  16:     {
  17:         document.write("You're using FF 1.x");
  18:     }
  19: }

Internet Explorer Browser Detection

   1: //test for MSIE x.x or Internet Exploerer;
   2: if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
   3: { 
   4:     var ieversion = new Number(RegExp.$1) ;
   5:     if (ieversion>=8)
   6:     {
   7:         document.write("You're using IE8 or above");
   8:     }
   9:     else if (ieversion >= 7)
  10:     {
  11:         document.write("You're using IE7.x");
  12:     }
  13:     else if (ieversion >= 6)
  14:     {
  15:         document.write("You're using IE6.x");
  16:     }
  17:     else if (ieversion >= 5)
  18:     {
  19:         document.write("You're using IE5.x");
  20:     }
  21: }

Opera Browser Detection

   1: //test for Opera/x.x or Opera x.x (ignoring remaining decimal places);
   2: if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent))
   3: { 
   4:      var oprversion = new Number(RegExp.$1); 
   5:      if (oprversion>=10)
   6:      {
   7:         document.write("You're using Opera 10.x or above");
   8:      }
   9:      else if (oprversion>=9)
  10:      {
  11:         document.write("You're using Opera 9.x");
  12:      }
  13:      else if (oprversion>=8)
  14:      {
  15:         document.write("You're using Opera 8.x");
  16:      }
  17:      else if (oprversion>=7)
  18:      {
  19:         document.write("You're using Opera 7.x");
  20:      }
  21: }

Thanks to the author for this code snippet. Hope its works for you as it works for me.



1 Comment »

  1. Abeon says:

    The Opera code doesn’t work for me.
    I get a parse error on line 2, but can’t seem to find why.

RSS feed for comments on this post. TrackBack URL

Leave a comment

viagra patent expire Viagra Sale viagra anxiety