User Agent Detection Logic

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fotios

    User Agent Detection Logic

    Hi guys,

    I have put together a flexible client-side user agent detector (written in
    js). I thought that some of you may find it useful. Code is here:



    The detector requires javascript 1.0 to work. This translates to netscape
    2.0 and IE 3.0 (although maybe IE 2.0 also works with it)

    Cheers,
    Fotios

    --






  • Lasse Reichstein Nielsen

    #2
    Re: User Agent Detection Logic

    "Fotios" <f_bass@yahoo.c om> writes:
    [color=blue]
    > I have put together a flexible client-side user agent detector (written in
    > js). I thought that some of you may find it useful. Code is here:
    >
    > http://fotios.cc/software/ua_detect.htm[/color]

    My only real problem with this code, is the problem it tries to solve.

    You are trying to make a white-list of allowed browsers. White-lists
    are not a very good idea, since they require constant updating. A
    black-list is better. If you give a warning for browsers that you know
    are *not* compatible with your page, and you write code that works
    with modern standards, then you will rarely have to update the list.
    That is, give an unknown browser the benefit of the doubt.

    You also base the detection solely on the navigator.userA gent string.
    That is not a reliable method, as some browsers are known to fake that
    string.

    There are some minor stuff:

    - You use parseInt with only one argument. (And which browser has the
    length of an array as a string?)

    - You use "new Array(elem1,ele m2,...,elemn);" , which doesn't seem to
    initialize the array in Netscape 2.02 (nor does the length property
    of the array work).

    - You write <script language="JavaS cript">, which is illegal in HTML 4+.
    The type attribute is required.

    [color=blue]
    > The detector requires javascript 1.0 to work. This translates to
    > netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)[/color]

    IE 2 didn't have scripting at all. That was introduced in IE 3.0b2,
    IIRC. I don't think the script is compatible with Netscape 2.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Richard Cornford

      #3
      Re: User Agent Detection Logic

      "Fotios" <f_bass@yahoo.c om> wrote in message
      news:3f6e26f9$0 $217$bed64819@n ews.gradwell.ne t...[color=blue]
      >I have put together a flexible client-side user agent detector (written
      >in js). I thought that some of you may find it useful. Code is here:
      >
      > http://fotios.cc/software/ua_detect.htm
      >
      >The detector requires javascript 1.0 to work. This translates to
      >netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)[/color]

      User agent detecting is unnecessary in almost all cases but of the
      methods that have been attempted the use of the browser's
      navigator.userA gent string is easily the most useless as very few
      current browsers report consistent or accurate information in this
      string. Often in order to avoid this type of clumsy and misguided
      detection categorising them as "unknown" or "declined" when their
      JavaScript and DOM support is entirely up to the requirements of the
      majority of scripts and their authors can see no reason for them to be
      excluded just because some script author only knows enough to recognise
      7 browsers by name.

      <URL: http://jibbering.com/faq/#FAQ4_26 >

      Richard.


      Comment

      • Fotios

        #4
        Re: User Agent Detection Logic

        Hi Lasse,

        thanks for taking the time to help me (or us) improve this code!
        [color=blue]
        >
        > My only real problem with this code, is the problem it tries to solve.
        >
        > You are trying to make a white-list of allowed browsers. White-lists
        > are not a very good idea, since they require constant updating. A
        > black-list is better. If you give a warning for browsers that you know
        > are *not* compatible with your page, and you write code that works
        > with modern standards, then you will rarely have to update the list.[/color]

        I understand what you are trying to say but my experience from web
        development says that if you want to play it safe you should only allow your
        code to run on specific browsers - the browsers you have tested it with.
        However, if you review the code you will see that is uses a list that is
        both black and white; i.e. you can have both white listed and black listed
        browsers (Black listing takes precedence)

        [color=blue]
        > That is, give an unknown browser the benefit of the doubt.[/color]

        If you check the code you will see there is an option just for that (i.e.
        whether you want to allow an unknown browser to continue or not)

        [color=blue]
        >
        > You also base the detection solely on the navigator.userA gent string.
        > That is not a reliable method, as some browsers are known to fake that
        > string.[/color]

        Well, let's say that I won't be interested in covering this case just yet.
        AFAIK all major browsers have a default user agent string that is
        distinctive in some way and the rest of them (not major) should as well.

        [color=blue]
        >
        > There are some minor stuff:
        >
        > - You use parseInt with only one argument. (And which browser has the
        > length of an array as a string?)[/color]
        [color=blue]
        >
        > - You use "new Array(elem1,ele m2,...,elemn);" , which doesn't seem to
        > initialize the array in Netscape 2.02 (nor does the length property
        > of the array work).[/color]

        You are right on both of these two remarks. The first one was due to my
        sloppy testing of NN 2.0; I was getting an error and I assumed this was due
        to wrong typing of the length property of arrays but further testing I just
        did shows that this property is simply undefined (thus the error).

        And yes, this style of Array initialization does not seem to work with NN
        2.0.

        So, what I still need to make this work under NN 2.0 is:
        - write a function that counts the length of an array in a way that is JS
        1.0 compatible.
        - initialize the array with arr[index] = value; type of statements

        I should have the revised version soon.

        BTW, there is a great old browser software archive here:
        A world community for web developers, evolt.org promotes the mutual free exchange of ideas, skills and experiences.


        [color=blue]
        >
        > - You write <script language="JavaS cript">, which is illegal in HTML 4+.
        > The type attribute is required.[/color]

        Thanks for letting me know. I added it.

        [color=blue]
        >
        >[color=green]
        > > The detector requires javascript 1.0 to work. This translates to
        > > netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)[/color]
        >
        > IE 2 didn't have scripting at all. That was introduced in IE 3.0b2,[/color]

        Yeah, some crooked hypothesis making on my part there. That was Netscape's
        golden era.

        Later,
        Fotios



        Comment

        • Fotios

          #5
          Re: User Agent Detection Logic

          [color=blue]
          >
          > User agent detecting is unnecessary in almost all cases but of the
          > methods that have been attempted the use of the browser's
          > navigator.userA gent string is easily the most useless as very few
          > current browsers report consistent or accurate information in this
          > string. Often in order to avoid this type of clumsy and misguided
          > detection categorising them as "unknown" or "declined" when their
          > JavaScript and DOM support is entirely up to the requirements of the
          > majority of scripts and their authors can see no reason for them to be
          > excluded just because some script author only knows enough to recognise
          > 7 browsers by name.[/color]

          I rarely read something as inacurate as what you have just written.

          What about the fact that most script that goes around does not run properly
          in more than one or two browsers? A detector is about "what is" not about
          "what ought to be".

          F.




          Comment

          • Richard Cornford

            #6
            Re: User Agent Detection Logic

            "Fotios" <f_bass@yahoo.c om> wrote in message
            news:3f6e4683$0 $214$bed64819@n ews.gradwell.ne t...[color=blue][color=green]
            >>User agent detecting is unnecessary in almost all cases but of the
            >>methods that have been attempted the use of the browser's
            >>navigator.use rAgent string is easily the most useless as very few
            >>current browsers report consistent or accurate information in this
            >>string. Often in order to avoid this type of clumsy and misguided
            >>detection categorising them as "unknown" or "declined" when their
            >>JavaScript and DOM support is entirely up to the requirements
            >>of the majority of scripts and their authors can see no reason for
            >>them to be excluded just because some script author only knows
            >>enough to recognise 7 browsers by name.[/color]
            >
            >I rarely read something as inacurate as what you have just written.[/color]

            So you haven't bothered to read the comp.lang.javas cript FAQ before
            posting then:-

            <URL: http://jibbering.com/faq/#FAQ4_26 >

            - as it says essentially the same thing. (Still, I don't expect you
            would consider that a document that is subject the scrutiny and review
            of all of the regular poster to this group as having anything accurate
            to say on the subject of browser scripting.)
            [color=blue]
            >What about the fact that most script that goes around does not run
            >properly in more than one or two browsers?[/color]

            "most script that goes around" is written (or more often cut-n-pasted)
            by people who do not really understand what they are doing and should
            not be taken as an example of best (and in many cases not even
            acceptable) practice.
            [color=blue]
            >A detector is about "what is" not about "what ought to be".[/color]

            So test "what is" not "what ought to be" which is what you are testing
            by assuming that the navigator.userA gent string ought to be a
            discriminating indicator of the type and version of a web browser. It is
            not and has not been for quite some time now.

            As it is your code will identify Konqueror 3 as IE, Netscape, Opera,
            Konqueror and unknown depending on which of the userAgent string I
            choose form the list of 20 odd provided in the drop-down in the
            preferences. While the same script will be absolutely convinced that my
            Palm OS web browser is IE 6, but a script that treats it as IE 6 will
            fail horribly. To qualify as a "detector" a script should be expected to
            produce discriminating results. The navigator.userA gent string just
            cannot provide that.

            But, as I said, it is almost never necessary to know the browser type or
            version. Feature detection is the preferred strategy. A script author
            should know what features a browser must support in order for a script
            to work. Testing for the existence of those features prior to execution
            allows a script to execute in any browser that supports them and if they
            are not available it can cleanly and harmlessly exit. A strategy that is
            only interested in "what is" available on the browser in question, but
            without any interest in which browser that actually is. Also a strategy
            that can work successfully in a completely unknown browser, exploiting
            any browser up to its ability to support the script.

            Richard.


            Comment

            • Fotios

              #7
              Re: User Agent Detection Logic

              [color=blue]
              > So you haven't bothered to read the comp.lang.javas cript FAQ before
              > posting then:-
              >
              > <URL: http://jibbering.com/faq/#FAQ4_26 >[/color]

              I do not need to read the FAQ before or after I post.
              [color=blue]
              >
              > - as it says essentially the same thing. (Still, I don't expect you
              > would consider that a document that is subject the scrutiny and review
              > of all of the regular poster to this group as having anything accurate
              > to say on the subject of browser scripting.)[/color]

              I don't know what to answer to you as I have not read the FAQ.
              [color=blue]
              >[color=green]
              > >What about the fact that most script that goes around does not run
              > >properly in more than one or two browsers?[/color]
              >
              > "most script that goes around" is written (or more often cut-n-pasted)
              > by people who do not really understand what they are doing and should
              > not be taken as an example of best (and in many cases not even
              > acceptable) practice.[/color]

              Isn't it still a fact that most script that goes around is not
              cross-browser?
              I have also heard that no scripter really knows what he/she is doing. What
              do you think about that?
              [color=blue]
              >[color=green]
              > >A detector is about "what is" not about "what ought to be".[/color]
              >
              > So test "what is" not "what ought to be" which is what you are testing
              > by assuming that the navigator.userA gent string ought to be a
              > discriminating indicator of the type and version of a web browser. It is
              > not and has not been for quite some time now.[/color]

              One can easily write a lynx-like user agent that supplies any user string
              he/she damn pleases.
              Why does that shatter the status of things? (The status being that all major
              browsers have - and should have - distinctive default strings)
              [color=blue]
              >
              > As it is your code will identify Konqueror 3 as IE, Netscape, Opera
              > Konqueror and unknown depending on which of the userAgent string I
              > choose form the list of 20 odd provided in the drop-down in the
              > preferences.[/color]

              Well, if you change the string you are on your own.
              [color=blue]
              > While the same script will be absolutely convinced that my
              > Palm OS web browser is IE 6, but a script that treats it as IE 6 will
              > fail horribly. To qualify as a "detector" a script should be expected to
              > produce discriminating results. The navigator.userA gent string just
              > cannot provide that.[/color]

              Only because you were naughty and you changed the string.
              What is the motivation in coding for the miniscule percentage of users who
              not only use virtually non-existent browsers but also play with their user
              agent strings?
              [color=blue]
              >
              > But, as I said, it is almost never necessary to know the browser type or
              > version. Feature detection is the preferred strategy. A script author
              > should know what features a browser must support in order for a script
              > to work.
              > Testing for the existence of those features prior to execution
              > allows a script to execute in any browser that supports them and if they
              > are not available it can cleanly and harmlessly exit. A strategy that is
              > only interested in "what is" available on the browser in question, but
              > without any interest in which browser that actually is. Also a strategy
              > that can work successfully in a completely unknown browser, exploiting
              > any browser up to its ability to support the script.[/color]

              1. Detecting a browser type is not necessarily for scripting purposes. For
              instance, you may not like the way something renders in gecko.

              2. I don't want to build a detector every time depending on what I want to
              use.

              3. A generic feature based browser detector seems to be much more work than
              the one I posted. If you build one let me know and I will give it a try.

              F.



              Comment

              • Fotios

                #8
                Re: User Agent Detection Logic

                Hi,

                I have amended the code in a few places in order to be Netscape 2.0 (JS 1.0)
                compatible:

                * The JS1.0 parseFloat() function needed some help as it returns 0 (instead
                of the NaN in later JS versions) when the initial char of the string is not
                a digit.
                There is now a helper function named: skipNonDigits()

                * The array is now populated in a way that is JS 1.0 compatible

                * I use substring() instead of substr(). This is because the former seems to
                be working better in Netscape 2.0 although both are supported by JS 1.0

                * In lack of the length property for arrays under JS 1.0 the length of the
                browser array needs to be specified by the user along with the rest of the
                parameters.

                Revised code here:



                Cheers,
                Fotios



                Comment

                • Ivo

                  #9
                  Re: User Agent Detection Logic

                  "Fotios" <f_bass@yahoo.c om> wrote in message
                  news:3f6e6b22$0 $216$bed64819@n ews.gradwell.ne t...[color=blue]
                  >[color=green]
                  > > So you haven't bothered to read the comp.lang.javas cript FAQ before
                  > > posting then:-
                  > >
                  > > <URL: http://jibbering.com/faq/#FAQ4_26 >[/color]
                  >
                  > I do not need to read the FAQ before or after I post.[/color]

                  You won't be jailed, but it seriously harms both your insight and our
                  sympathy if you don't.
                  [color=blue]
                  > I have also heard that no scripter really knows what he/she is doing. What
                  > do you think about that?[/color]

                  No one can speak but for themselves, especially if they spend their time
                  behind computer screens. I for one haven't a clue.
                  [color=blue][color=green]
                  > > While the same script will be absolutely convinced that my
                  > > Palm OS web browser is IE 6, but a script that treats it as IE 6 will
                  > > fail horribly. To qualify as a "detector" a script should be expected to
                  > > produce discriminating results. The navigator.userA gent string just
                  > > cannot provide that.[/color]
                  >
                  > Only because you were naughty and you changed the string.
                  > What is the motivation in coding for the miniscule percentage of users who
                  > not only use virtually non-existent browsers but also play with their user
                  > agent strings?[/color]

                  I started reading this thread with a positive feeling, but this turns sour.
                  What is the movitation behind writing a script that scrutinizes something so
                  irrelevant? The bottom line is the navigator agent string thing can be
                  changed at will both by browser-makers and end-users. Mine is a Shakespeare
                  quote.
                  If I want my script to bark up the document.images tree, I will not test for
                  document.styleS heets or screen.availWid th or navigator.anyTh ing.

                  Regards,
                  Ivo


                  Comment

                  • HikksNotAtHome

                    #10
                    Re: User Agent Detection Logic

                    In article <3f6e8b70$0$356 81$1b62eedf@new s.wanadoo.nl>, "Ivo" <no@thank.you >
                    writes:

                    <snip>
                    [color=blue][color=green]
                    >> I do not need to read the FAQ before or after I post.[/color]
                    >
                    >You won't be jailed, but it seriously harms both your insight and our
                    >sympathy if you don't.[/color]

                    Not to mention credibility. He multi-posted these same thoughts in the
                    microsoft.publi c.scripting.jsc ript NG
                    <snip>
                    [color=blue][color=green]
                    >> Only because you were naughty and you changed the string.
                    >> What is the motivation in coding for the miniscule percentage of users who
                    >> not only use virtually non-existent browsers but also play with their user
                    >> agent strings?[/color]
                    >
                    >I started reading this thread with a positive feeling, but this turns sour.[/color]

                    You too?

                    <snip>

                    --
                    Randy

                    Comment

                    • Fotios

                      #11
                      Re: User Agent Detection Logic

                      [color=blue]
                      >
                      > You won't be jailed, but it seriously harms both your insight[/color]

                      maybe, but it helps uncalcify my thinking.
                      [color=blue]
                      > and our
                      > sympathy if you don't.[/color]

                      awwwww! Please? Pretty Please?

                      [color=blue]
                      >[color=green]
                      > > I have also heard that no scripter really knows what he/she is doing.[/color][/color]
                      What[color=blue][color=green]
                      > > do you think about that?[/color]
                      >
                      > No one can speak but for themselves, especially if they spend their time
                      > behind computer screens. I for one haven't a clue.[/color]

                      This philosophy of yours is very deep.

                      [color=blue]
                      > The bottom line is the navigator agent string thing can be
                      > changed at will both by browser-makers and end-users. Mine is a[/color]
                      Shakespeare[color=blue]
                      > quote.[/color]

                      my friend, you are too kewl!

                      [color=blue]
                      > If I want my script to bark up the document.images tree, I will not test[/color]
                      for[color=blue]
                      > document.styleS heets or screen.availWid th or navigator.anyTh ing.[/color]

                      In your limited universe (browser detection) = (browser detection for
                      javascript running purposes)

                      [color=blue]
                      >
                      > Regards,
                      > Ivo[/color]

                      More regards,
                      Fotios


                      Comment

                      • Fotios

                        #12
                        Re: User Agent Detection Logic

                        > Not to mention credibility. He multi-posted these same thoughts in the[color=blue]
                        > microsoft.publi c.scripting.jsc ript NG
                        > <snip>[/color]

                        I do not see what credibility has to do with posting in two groups instead
                        of one.
                        Then again, if it is so, I wonder how this original theory of yours applies
                        to multi-NG whiners.

                        [color=blue]
                        > You too?[/color]

                        hey, you found company! You guys should build on this.

                        F.



                        Comment

                        • Steve van Dongen

                          #13
                          Re: User Agent Detection Logic

                          On Mon, 22 Sep 2003 06:23:59 +0100, "Fotios" <f_bass@yahoo.c om> wrote:
                          [color=blue]
                          >Isn't it still a fact that most script that goes around is not
                          >cross-browser?[/color]

                          Who knows.
                          [color=blue]
                          >I have also heard that no scripter really knows what he/she is doing. What
                          >do you think about that?[/color]

                          I think that sounds like flame bait. I think that if you believe
                          that, then you just insulted yourself. I think you need better
                          sources of information. I think that if you stick around the
                          newsgroup for a while you will discover that statement is wrong.
                          [color=blue]
                          >2. I don't want to build a detector every time depending on what I want to
                          >use.[/color]

                          Yeah, if you want to test whether the browser is capable of doing,
                          say, image rollovers, writing
                          var fSupportsScript ableImages = (document.image s ? true : false);
                          sure is a real pain.
                          [color=blue]
                          >3. A generic feature based browser detector seems to be much more work than
                          >the one I posted. If you build one let me know and I will give it a try.[/color]

                          No one would ever write a "generic feature based browser detector".
                          Whatever that is. You don't need to know every feature the browser
                          supports; you only need to know if it supports the specific feature(s)
                          you are trying to use.

                          This isn't an argument you'll win; feature/object detection has long
                          been recommended over browser detection. It's less prone to error and
                          it requires less maintenance because it's backward and FORWARD
                          compatible.

                          Regards,
                          Steve

                          Comment

                          • Dr John Stockton

                            #14
                            Re: User Agent Detection Logic

                            JRS: In article <3f6e6b22$0$216 $bed64819@news. gradwell.net>, seen in
                            news:comp.lang. javascript, Fotios <f_bass@yahoo.c om> posted at Mon, 22
                            Sep 2003 06:23:59 :-[color=blue]
                            >[color=green]
                            >> So you haven't bothered to read the comp.lang.javas cript FAQ before
                            >> posting then:-
                            >>
                            >> <URL: http://jibbering.com/faq/#FAQ4_26 >[/color]
                            >
                            >I do not need to read the FAQ before or after I post.[/color]

                            It would, however, be a useful precaution against appearing to be a
                            person of inadequate sagacity. Of course, that appearance would in that
                            case not be misleading.

                            --
                            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                            Comment

                            • Fotios

                              #15
                              Re: User Agent Detection Logic

                              [color=blue]
                              >[color=green]
                              > >I have also heard that no scripter really knows what he/she is doing.[/color][/color]
                              What[color=blue][color=green]
                              > >do you think about that?[/color]
                              >
                              > I think that sounds like flame bait. I think that if you believe
                              > that, then you just insulted yourself. I think you need better
                              > sources of information. I think that if you stick around the
                              > newsgroup for a while you will discover that statement is wrong.[/color]

                              Looks like you are biting. BTW, I have been around this group for years.
                              [color=blue]
                              >
                              > Yeah, if you want to test whether the browser is capable of doing,
                              > say, image rollovers, writing
                              > var fSupportsScript ableImages = (document.image s ? true : false);
                              > sure is a real pain.[/color]

                              Wouldn't you rather skip that part?
                              [color=blue]
                              > No one would ever write a "generic feature based browser detector".
                              > Whatever that is.[/color]

                              Yeah, I suppose it is not mentioned in the FAQ; so FAQ puppies get
                              short-circuited.
                              [color=blue]
                              >
                              > This isn't an argument you'll win; feature/object detection has long
                              > been recommended over browser detection.[/color]

                              This world is not black and white. Browser detection using the useragent
                              string still has a place and a function for the reasons I mentioned and
                              without claiming it is a panacea.

                              BTW, I am still waiting for your answer on the rest of my points about why
                              one would use useragent string based detection.

                              F.


                              Comment

                              Working...