Skip to content Skip to sidebar Skip to footer

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 ent

Solution 1:

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

Solution 2:

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?

Solution 3:

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.

Solution 4:

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.

Solution 5:

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 ;)

Post a Comment for "Reliable Browser Detection With Javascript?"