Results 1 to 10 of 10

Thread: Detect Firefox Javascript

  1. #1
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Detect Firefox Javascript

    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.

    Code:
    <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
    Last edited by cr3ative; 12-25-2004 at 06:21 PM.
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by cr3ative
    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).

    Quote Originally Posted by cr3ative
    HTML Code:
    <script>
    Valid HTML requires a type attribute:

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

    Merry Christmas,
    Mike

  3. #3
    Join Date
    Aug 2004
    Location
    Brighton
    Posts
    1,563
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mwinter
    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
    A retired member, drop me a line through my site if you'd like to find me!
    cr3ative media | read the stickies

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by cr3ative
    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.

  5. #5
    Join Date
    Sep 2004
    Location
    Little Falls, Roodepoort, South Africa
    Posts
    421
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    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

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by simonf
    I would like to politely remind you of the rules
    I am well aware of the rules and I haven't broken them.

    Mike; you come across as a very aggressive person
    I 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

  7. #7
    Join Date
    Sep 2004
    Location
    Little Falls, Roodepoort, South Africa
    Posts
    421
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    Hi Mike

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

  8. #8
    Join Date
    Jan 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Bravo!!

  9. #9
    Join Date
    Jan 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mwinter View Post
    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.

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by g33k View Post
    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.
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •