basic javascript syntax question

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

    basic javascript syntax question

    I know that this works. I just don't get the syntax.


    I know its checking the OS. just not sure how it works.

    var v = navigator.appVe rsion.toUpperCa se()
    if (1+v.indexOf('W IN98')
    os = 'Win98';
    else if (1+v.indexOf('W INNT')
    os = 'WinnT';

    etc....

    ok I know that v is set to whatever the value of the OS is. However,
    'indexof' is the kind of function used with an array that tells you
    what index a value is at. Why is ther '1+' and where is the '='. I
    dont see an if check.
  • Michael Winter

    #2
    Re: basic javascript syntax question

    On 19 Sep 2004 07:53:38 -0700, Ryan Gaffuri <rgaffuri@cox.n et> wrote:
    [color=blue]
    > I know that this works. I just don't get the syntax.
    >
    >
    > I know its checking the OS. just not sure how it works.[/color]

    The first thing to mention is that browser-sniffing code should be
    avoided. It results in fragile code that is unreliable. Furthermore, it is
    based on information that the user agent chooses to provide. This is not
    always the same as the true information. Many lesser-known browsers do
    this because clueless authors screen their sites for certain browsers and
    reject all the others.

    If you need to determine the capabilities of a browser, use feature
    detection. That, and other information is available in the group FAQ:

    <URL:http://jibbering.com/faq/>
    [color=blue]
    > var v = navigator.appVe rsion.toUpperCa se()
    > if (1+v.indexOf('W IN98')
    > os = 'Win98';
    > else if (1+v.indexOf('W INNT')
    > os = 'WinnT';[/color]

    Ignoring the syntax errors[1], this code is quite simple. The if statement
    evaluates the expression within parentheses as a boolean (true/false). The
    boolean equivalent of a value depends on the type.

    Type False True

    Boolean false true
    Number Zero (0) Any number except zero (including
    negative values)
    String Empty string Any string with a length >= 1
    Object Null reference Any object reference
    Null Always false
    Undefined Always false

    The indexOf method returns the position of a given string within another
    string. As in the majority of cases, indicies start from zero, so -1 is
    used to indicate that the substring couldn't be found.

    If you apply the "+ 1" knowing the above, you can see that if the strings
    'WIN98' and 'WINNT' can't be found, the if statement will evaluate (1
    + -1) which is false (0). If the strings can be found, indexOf will return
    a number >= 0, which after adding 1 will always evaluate as true.

    [snip]

    Hope that helps,
    Mike


    [1] The if statements are missing closing parentheses. The code should
    look something like:

    if (1 + v.indexOf('WIN9 8')) {
    os = 'Win98';
    } else if (1 + v.indexOf('WINN T')) {
    os = 'WinnT';
    }

    The braces in this instance are a style preference, but a good idea in the
    event that the single statements become blocks.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Lee

      #3
      Re: basic javascript syntax question

      Ryan Gaffuri said:[color=blue]
      >
      >I know that this works. I just don't get the syntax.
      >
      >
      >I know its checking the OS. just not sure how it works.
      >
      >var v = navigator.appVe rsion.toUpperCa se()
      >if (1+v.indexOf('W IN98')
      >os = 'Win98';
      >else if (1+v.indexOf('W INNT')
      >os = 'WinnT';
      >
      >etc....
      >
      >ok I know that v is set to whatever the value of the OS is. However,
      >'indexof' is the kind of function used with an array that tells you
      >what index a value is at. Why is ther '1+' and where is the '='. I
      >dont see an if check.[/color]

      The String.indexOf( ) method returns the position in the String
      at which the argument is found. If the argument is not found in
      the string at all, it returns -1.

      When you add 1 to that returned value, the result will be zero
      if the string is not found, or some positive number if it has
      been found.

      The "if" statement doesn't need an "==". It just needs an
      expression that can be evaluated as true or false. If you give
      it a number, it will consider it to be false if it is zero,
      and true if it is any other number.

      Comment

      • Robert

        #4
        Re: basic javascript syntax question

        In article <1efdad5b.04091 90653.627dd197@ posting.google. com>,
        rgaffuri@cox.ne t (Ryan Gaffuri) wrote:
        [color=blue]
        > var v = navigator.appVe rsion.toUpperCa se()
        > if (1+v.indexOf('W IN98')
        > os = 'Win98';
        > else if (1+v.indexOf('W INNT')
        > os = 'WinnT';[/color]

        These if statements are not valid. An ending ) is needed.
        [color=blue]
        >. However,
        > 'indexof' is the kind of function used with an array that tells you
        > what index a value is at.[/color]
        indexOf finds the starting position of a query string inside of the
        target string.
        [color=blue]
        > Why is ther '1+' and where is the '='. I
        > dont see an if check.[/color]

        JavaScript uses the c style syntax.

        This means
        1) the array index starts with 0
        2) the string index starts with 0
        3) the = always means an assignment even inside of an if statement
        4) the == test for equality
        5) the if statement has a short cut comparison built in. if (number) is
        like if (number != 0) or if (boolean) like if (boolean == true) or etc.
        [color=blue]
        > if (1+v.indexOf('W IN98'))[/color]

        since indexOf return a number from zero on up when the query string is
        found and -1 when the query string isn't found. The author added 1 to
        the found value to make a number comparison take the true path in the if
        statement when the string is found and the false path when the string
        isn't found.

        I'd have written
        if (v.indexOf('WIN 98') >= 0)

        Some c programmers like to be "clever" and do things in a non-obvious
        way. They may do this in the name of performance, but working code
        performs better than non-working code.

        I find c style syntax more cryptic than I would like. Fortunately,
        Javascript has eliminate the worst of the problems.

        You may want to pick up a book on Javascript. Consider Javascript: The
        Definitive Guide by David Flanagan. You may also want to buy one of the
        other books on Javascript.

        if (1+v.indexOf('W IN98'))

        The technique of checking for operating system the browser is running in
        has proven to be unreliable. Many browsers fib on what the running
        environment is to let them into sites.

        A better method is called feature detection. That is if you want to use
        the function getElementById, you do a test for it before using it. For
        example:
        var myValue;
        if (document.getEl ementById )
        { myValue = document.getEle mentById(theID) .value; }

        Robert

        Comment

        • Grant Wagner

          #5
          Re: basic javascript syntax question

          Ryan Gaffuri wrote:
          [color=blue]
          > I know that this works. I just don't get the syntax.
          >
          > I know its checking the OS. just not sure how it works.
          >
          > var v = navigator.appVe rsion.toUpperCa se()
          > if (1+v.indexOf('W IN98')
          > os = 'Win98';
          > else if (1+v.indexOf('W INNT')
          > os = 'WinnT';
          >
          > etc....
          >
          > ok I know that v is set to whatever the value of the OS is.[/color]

          Actually, v is set to the value of navigator.appVe rsion.toUpperCa se(),
          which may or may not be the version of the browser your JavaScript is
          running in, and may or may not contain the operating system being used.
          Here are my values of navigator.appVe rsion.toUpperCa se():

          IE6SP1: 4.0 (COMPATIBLE; MSIE 6.0; WINDOWS NT 5.1; .NET CLR 1.1.4322)
          Firefox 1.0PR: 5.0 (WINDOWS; )
          Mozilla 1.7.3: 5.0 (WINDOWS; EN-US)
          Opera 7.54 (set to "Identify as MSIE 6.0"): 4.0 (COMPATIBLE; MSIE 6.0;
          WINDOWS NT 5.1)
          Opera 6.05 (set to "Identify as MSIE 5.0"): 4.0 (COMPATIBLE; MSIE 5.0;
          WINDOWS XP)
          Netscape 4.78: 4.78 [EN]C-CCK-MCD 20011025 (WINDOWS NT 5.0; U) (it's a
          custom build install using Netscape's CCK)

          This is by no means a complete list of browsers in use, just a sampling
          to demonstrate the variety of responses you can obtain from
          navigator.appVe rsion. In each tested browser, the tests being done for
          WIN98 and WINNT will not match anything in
          navigator.appVe rsion.toUpperCa se().

          navigator.platf orm.toUpperCase () reports "WIN32" for each of the
          browsers listed above (when running on Windows)

          navigator.appCo deName.toUpperC ase() reports "MOZILLA" for each of the
          browsers listed above

          navigator.userA gent.toUpperCas e() provides slightly more information for
          a couple of browsers:
          Firefox 1.0PR: MOZILLA/5.0 (WINDOWS; U; WINDOWS NT 5.1; RV:1.7.3)
          GECKO/20040913 FIREFOX/0.10
          Mozilla 1.7.3: MOZILLA/5.0 (WINDOWS; U; WINDOWS NT 5.1; EN-US; RV:1.7.3)
          GECKO/20040910

          But navigator.userA gent is highly unreliable since it can be one of 5
          values in Opera, and practically anything in Firefox/Mozilla.

          In general, any information obtained by any property of the navigator
          object is of little use on the public Internet. It may be of slightly
          more value in the controlled environment of a corporate Intranet, where
          you can be assured that a particular appVersion or userAgent actually
          means the user is using a particular Windows version or
          navigator.langu age means that the corporate user is in a particular part
          of the world.

          --
          Grant Wagner <gwagner@agrico reunited.com>
          comp.lang.javas cript FAQ - http://jibbering.com/faq

          Comment

          Working...