PDA

View Full Version : Detect Firefox Javascript



cr3ative
12-13-2004, 11:20 AM
This script detects for the presence of FireFox and displays an image accordingly - either Get Firefox or Spread Firefox, for non-detection and detection.

edit: Forgot my script tag format, cheers Mike.


<script type="text/javascript">
if (navigator.userAgent.indexOf("Firefox")!=-1)
document.write("<img src='http://img46.exs.cx/img46/3493/spreadfirefox180600mv.gif'>")
if (navigator.userAgent.indexOf("MSIE")!=-1)
document.write("<img src='http://img33.exs.cx/img33/3916/getfirefox180604pu.gif'>")
</script>
See it in action at www.afpsupport.r8.org (the top right image)

cr3ative

mwinter
12-25-2004, 03:35 PM
This script detects for the presence of FireFox [...]No, it doesn't. It checks the user agent string for a "Firefox" or "MSIE" sub-string. That may, or may not, indicate the presence of Mozilla Firefox or Internet Explorer.

Browser detection is unreliable and it should only be attempted if absolutely necessary (and it's usually not).



<script>Valid HTML requires a type attribute:


<script type="text/javascript">Please don't recommend tag soup.

Merry Christmas,
Mike

cr3ative
12-25-2004, 07:59 PM
No, it doesn't. It checks the user agent string for a "Firefox" or "MSIE" sub-string. That may, or may not, indicate the presence of Mozilla Firefox or Internet Explorer.

Well, picky. :) It does in effect check for FireFox's presence, as it checks the agent string. I also can't really see why it'd be unreliable (or I may well be overseeing something major.. :()

Cheers
cr3ative

mwinter
12-26-2004, 12:10 AM
Well, picky. :)Maybe. I find it to be an important distinction.


I also can't really see why it'd be unreliable (or I may well be overseeing something major.. :()There is no standard or rule that states that the user agent string must be accurate. In fact, some user agents intentionally misrepresent themselves. This is born out of idiotic attempts by some document authors to reject a visitor based solely on the browser they use[1]. As proof of the unreliability, your script can detect my browser as IE, even though it most certainly isn't.

When there are fatal flaws in a browser implementation that cannot be avoided, one may attempt to detect that browser and treat it accordingly. However, most browser detection attempts are for needless reasons - such as coping with minor differences in the client-side object model - that can be overcome by much more robust means.

Mike


[1] This is usually because the author is too incompetent to create a HTML document correctly so rather than improve their work, they block the affected users from the site.

simonf
01-06-2005, 01:23 PM
Mike Winter

I would like to politely remind you of the rules:

1.1) Always be respectful and polite when posting- There is absolutely no excuse to be rude, confrontational, or hostile when posting on Dynamic Drive. This is a place where web programmers come together to solve problems, not create them.

Mike; you come across as a very aggressive person, and it makes you sounds as if you have a major chip on your shoulder, which I'm sure is not the case. Try cooling the replies down and show a little respect or tolerance to other members that try and assist, even if their answer is not 100%, there are far more effective ways to respond!! Whilst I apologise... if the message offends you, some of your comments offend me and there not even aimed at myself. Best rgds,Simonf:cool:

mwinter
01-06-2005, 02:36 PM
I would like to politely remind you of the rulesI am well aware of the rules and I haven't broken them.


Mike; you come across as a very aggressive personI am terse, but that is far from aggressive.

At no point, in this thread or others, have I attacked or insulted any members of this forum. If you have read anything differently, it is merely your interpretation and not my intent.

Mike

simonf
01-06-2005, 02:42 PM
Hi Mike

Terse: abrupt, short, curt, brusque, snappish, clipped, concise... just the point ;)

martinterry
01-25-2005, 04:51 PM
Bravo!!

g33k
01-24-2008, 08:57 PM
Maybe. I find it to be an important distinction.

There is no standard or rule that states that the user agent string must be accurate. In fact, some user agents intentionally misrepresent themselves. This is born out of idiotic attempts by some document authors to reject a visitor based solely on the browser they use[1]. As proof of the unreliability, your script can detect my browser as IE, even though it most certainly isn't.

When there are fatal flaws in a browser implementation that cannot be avoided, one may attempt to detect that browser and treat it accordingly. However, most browser detection attempts are for needless reasons - such as coping with minor differences in the client-side object model - that can be overcome by much more robust means.

Mike


[1] This is usually because the author is too incompetent to create a HTML document correctly so rather than improve their work, they block the affected users from the site.

While I understand where you are coming from, there ARE instances where browser detection is desirable. As long as the user keeps in mind that it may not be 100% accurate, I see no problem with using this type of code. I recently ran into an issue where Firefox did not display something correctly unless I used a particular CSS tag that IE did not support. Using the code above allowed me to write the page so that it would look correct in both browsers for 99.9% of users. If users are messing with their browser ID string, then they can deal with a messed-up looking page! :) Of course, all of this would be a moot point if IE would just comply to standards. :rolleyes:

jscheuer1
01-24-2008, 09:14 PM
While I understand where you are coming from, there ARE instances where browser detection is desirable. As long as the user keeps in mind that it may not be 100% accurate, I see no problem with using this type of code. I recently ran into an issue where Firefox did not display something correctly unless I used a particular CSS tag that IE did not support. Using the code above allowed me to write the page so that it would look correct in both browsers for 99.9% of users. If users are messing with their browser ID string, then they can deal with a messed-up looking page! :) Of course, all of this would be a moot point if IE would just comply to standards. :rolleyes:

That can be 100% reliably done with no javascript or browser detection by using IE conditional comment tags.

Conditional comments may seem like browser detection, but unlike it, they are a documented supported feature. Browser detection relies upon quirks of the browser interface. Feature detection is another reliable and 100% applicable method, as long as it is well thought out and the code involved is javascript only, as it relies upon javascript being enabled.