user188962
user188962

Reputation:

Reliable browser detection with javascript?

I have a website, and offcourse like most websites it loads faster in Firefox.

I want to create some form of a bar, which displays on top of my site, whenever a user without FF enters the site. This bar will say something like "Install FF for better performance etc etc..."

How should I do this? Browser detection I guess, but there are tons of different codes out there. Any code you all know that is more reliable, and better to use?

Btw, does anybody know if I need permission from Mozilla to have such a bar on my website?

Thanks

EDIT: FF IS NOT THE FASTEST BROWSER, I GOT IT!

Upvotes: 5

Views: 2854

Answers (8)

Yi Jiang
Yi Jiang

Reputation: 50095

You may be interested in this page here on spreadfirefox.com: http://www.spreadfirefox.com/affiliates/utw. It contains a set of Spread Firefox buttons, but what's more interesting is the code that comes with it. You'll notice that the button's image will actually change depending on which browser you view it on:


(source: mozilla.org)


(source: mozilla.org)


(source: mozilla.org)


(source: mozilla.org)

Looking at the Javascript code, you can see that they're already doing the work for you:

/* *********************************************
This code came from http://www.quirksmode.org/js/detect.html 
In order for this to function properly, it must be updated regularly
***********************************************/

So why not just modify the code they've given to work with your images or code? Or better yet, use one of their ready made ones?

Upvotes: 7

Alin P.
Alin P.

Reputation: 44346

(Skipping all the arguments on whether what you want to do is a good or a bad thing. Assuming you really like Firefox and want to promote it.)

  1. Use the navigator object to check if the browser is Firefox. navigator.appCodeName, navigator.appName, navigator.appVersion. What you want to do is not a critical part of your website, so you don't need a fail-safe detection method. If it works for over 95% of your targeted users than the method is good.

  2. I doubt you need permission to advertise a free product. Mozilla even encourages you to do so.

Upvotes: 10

tbleckert
tbleckert

Reputation: 3803

Ok so I get a -1 because I don't like Firefox or because I didn't give you the solution in your hand for you to copy and paste? Well here you go people:

<!--[if IE]>
Special instructions for IE
<![endif]-->

And in jQuery

if ($.browser.msie)

Upvotes: 2

jman
jman

Reputation: 474

This might not answer your question, but you can make conditional comments in your HTML code like this:

http://www.quirksmode.org/css/condcom.html

You can use this code to detect any IE version.

Unless you are hired by the Mozilla Foundation, I don't see why users with Chrome and Safari and similar browsers should be notified with this message. They made an active choice and picked their own browser, something you can't say about most IE users who might be unaware of the alternatives. Mainly, it's Internet Explorer that is the problem.

Sorry if this was slightly off-topic and if it wasn't helpful at all.

/ end of moral rant ;)

Upvotes: 4

Dr.Molle
Dr.Molle

Reputation: 117314

There is no 100% reliable browser detection. Accessing the userAgent-string provided by the navigator-object is not reliable while it can be faked by the user. Accessing some properties that only exist in a special browser now, may be reliable right now, but not in the future, because you never know, if another browser will apply this property sometimes or the property will be removed from the browser in future versions.

Upvotes: 4

fcalderan
fcalderan

Reputation:

I think browser detection is not the right way to solve some issues. If your code is slow on other browser you should first investigate the reason, clean and optimize your code, then if you don't solve you could optionally provide a reduced functionality for other browser.

But please, avoid suggesting user what he should install in its computer

Upvotes: 2

littlegreen
littlegreen

Reputation: 7420

jQuery has browser detection.

The jQuery dev's themselves however argue that you shouldn't select on browser, but on what features are supported by the browser. Thus they recommend browser feature detection instead. Makes sense.

Upvotes: 6

darioo
darioo

Reputation: 47183

I don't think that's such a good idea. What about other browsers such as Chrome or Safari? Right now, Chrome is faster than Firefox (Sunspider tests), so your statement "it loads faster in Firefox" is not really correct. Faster than what? If you mean IE, then there are already solutions around the web for alerting users not to use IE and switch to something else. You shouldn't favorize just one browser.

Upvotes: 1

Related Questions