-1

Possible Duplicate:
Determine what Browser being used, using javascript

I'm trying to implement a method in JavaScript which would detect the User's browser for starters and print it.

I already know about the Navigator Object in JavaScript, but I do not want to use that. Is there any other way of doing it?

Thanks.

Community
  • 1
  • 1

5 Answers5

3

If you don't want to use the navigator property you are out of luck because this is the only way to detect the user agent in javascript.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Well it's hardly the only way... One could write a library to test for browser specific traits, but the navigator property is the only easy and reliable way – Hubro May 09 '12 at 07:59
  • Opera and Chrome have an object for that "purpose". It is awkward, but it is there. – Bakudan May 09 '12 at 08:05
0

You could use jQuery.browser (which indirectly uses the navigator object I guess, but you don't have to touch it)

Example:

<!DOCTYPE html>
<html>
<head>
  <style>
  p { color:green; font-weight:bolder; margin:3px 0 0 10px; }
  div { color:blue; margin-left:20px; font-size:14px; }
  span { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<p>Browser info:</p>

<script>
    jQuery.each(jQuery.browser, function(i, val) {
      $("<div>" + i + " : <span>" + val + "</span>")
                .appendTo( document.body );
    });</script>

</body>
</html>

Output:

Browser info:
webkit : true
version : 536.10
safari : true
Hubro
  • 56,214
  • 69
  • 228
  • 381
0

You could use jQuery's $.browser [1] functionality.

Or something like CSS Browser Selector [2] to add CSS classes to the <html> element

[1] http://api.jquery.com/jQuery.browser/

[2] http://rafael.adm.br/css_browser_selector/

James Simm
  • 1,569
  • 1
  • 16
  • 28
0

if you don/t want to use Navigator then user jquery Here is detaied about it.

jQuery.browser

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

In some cases you can directly test for the method(s) that you need to use. For example, while current versions of all the browsers support the window.addEventListener method, you may want to detect whether that method exists and if not attempt some other way to listen to the event that you want.

Neil
  • 54,642
  • 8
  • 60
  • 72