views:

177

answers:

11
if(firefox and is on a computer){
alert('using firefox on a computer')
}else{
alert("using something else!");
}

How can I do this?

+3  A: 

You tagged this jquery, so there is jquery.browser.

Example from the manual page:

jQuery.each(jQuery.browser, function(i, val) {
   if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.9")
      alert("Do stuff for firefox 3")
 });

they recommend against checking for specific browsers, though, and say to use feature detection instead.

Pekka
A: 

Use a real library like jquery etc, then you really shouldnt need to worry about this most of the time.

mP
-1 — recommending a library is fine, recommending a library that is already listed in the tags is pointless, not describing how to solve the problem with said library is less than helpful.
David Dorward
Only depending on a library instead of trying to understand the language itself is a bad thing. You must know some javascript too.
poo
A: 

You can use navigator.userAgent for this. Just see if it contains Mozilla

Larry_Croft
Pretty much everything includes the string *Mozilla* … including Safari on the iPhone! This is useless for determining if Firefox is being used, it is useless for determining if a computer is being used!
David Dorward
Have a look at http://www.nczonline.net/blog/2010/01/12/history-of-the-user-agent-string for a detailed history of the infamous *Mozilla* tag.
Marcel Korpel
+1  A: 

http://api.jquery.com/jQuery.browser/

if ($.browser.mozilla) { ...
nandu
+1  A: 

You can make the control with javascript's navigator.userAgent or navigator object in general,

But if you want to use something ready to go, check this:

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

hope this helps, Sinan.

Sinan Y.
+1  A: 

It's better to detect features you need, not a browser. For example, if you need to know if foo() is supported, you can check it with if(foo){}

Sam Dark
+6  A: 

What you're after is known as browser detection:

if ($.browser.mozilla) { ... 

However, browser sniffing is discouraged, as its easy to spoof the user agent, i.e. pretend to be another browser!

You'd best use feature detection, either in your own way, or through the jQuery.support interface: http://api.jquery.com/jQuery.support/

Here's an article on extending it for your own use: http://www.waytoocrowded.com/2009/03/14/jquery-supportminheight/

Edit:

Found this post as well which helps: http://stackoverflow.com/questions/1298713/when-ie8-is-not-ie8-what-is-browser-version/1298738#1298738

James Wiseman
You mean discouraged? ;) +1 for feature detection, please do it 'the right way': http://www.nczonline.net/blog/2009/12/29/feature-detection-is-not-browser-detection/
Marcel Korpel
@Marcel Korpel: Haha, thanks!
James Wiseman
You beat me to it Marcel :)
Olly Hodgson
@Marcel Korpel: Brilliant article, by the way. I realised that I didn't properly understand feature detection. Have you posted that in an answer? Because I'd happily vote it up!
James Wiseman
A: 
navigator.sayswho= (function(){
  var N= navigator.appName, ua= navigator.userAgent, tem;
  var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
  if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
  M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
  return M.join(' ');
 })();

as the name suggests, this is who the browser says it is- but use object detection before asking it to actually do anything...

I use it for logging errors from users and in testing code in multiple browsers- where I know the userAgent strings.

kennebec
A: 

Like this: Check for Firefox. Or some other browser.

 window.onload = function() {
          //  alert(navigator.userAgent);
            if (navigator.userAgent.indexOf("Firefox") > 0) {
                alert("ff");
            }
        }
poo
+1  A: 

As already asked in a comment: why do you want this? Browser sniffing is a bad habit and there are only a few situations where it is needed.

Instead, use feature detection. As described by Nicholas Zakas, you should test relatively 'uncommon' features before using them and only rely on these tests, so you're kind of fail-safe. For example, do

if (window.XMLHttpRequest)
    var xhr = new XMLHttpRequest();

instead of

if ((brwsr.IE && brwsr.IE.version >= 7) || (brwsr.firefox) || (brwsr.opera))
    var xhr = new XMLHttpRequest();

And also don't do

if (window.XMLHttpRequest)
    // Hey, native XMLHttpRequest-support, so position: fixed is also supported

(instead, test if position: fixed is supported)

There exist several uncommon browsers with names like Kazehakase and Midori that also might, or might not, support these features, so your scripts will silently work on them when using feature detection.

But please read the mentioned article, as it contains a very good and thorough explanation of this technique. (By the way, I think that Zakas' Professional JavaScript for Web Developers is still too unknown.)

Marcel Korpel
+1  A: 

Usually you'd like to separate IE from all the others, because "the others" behave all almost the same way (read: according the standards). The shortest and most reliable way for this is using the IE's conditional compilation feature:

var IE = /*@cc_on!@*/false;

which can be used as

if (IE) {
    // IE.
} else {
    // Others.
}

In IE, the ! will be compiled and taken in the expression, resulting in a new expression !false, which is logically true. We could also use the well known document.all check here, but this would evaluate to true in certain Opera versions as well.

If you really, really insist in detecting FireFox (actually: the Gecko/Mozilla engine) only, then just detect if a Gecko/Mozilla specific property exists:

var FF = !(window.mozInnerScreenX == null);

which can be used as

if (FF) {
    // FF.
} else {
    // Others.
}
BalusC