help please - script problem in IE 6 - (but not in Mozilla/Firefox)

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

    help please - script problem in IE 6 - (but not in Mozilla/Firefox)

    I have the following script in a page and it gets an error in IE 6. Says
    something about an invalid
    argument but the line number doesn't help since I can't see the javascript
    code when viewing source.
    I have this script in a popup page where the user selects a user ID (id) and
    that value is then placed
    into one of the parent window's form text field called "userId". This
    script works fine in the latest
    Mozilla and Mozilla Firefox but not in IE 6. Any help on why this script
    might not work or a
    better way of accomplishing this task would be appreciated. Thank you.
    derek

    <script language="javas cript">

    function userSelected(id )
    {
    var w = window.opener;
    if (w && !w.closed)
    {
    if (w.document)
    {
    var e = w.document.getE lementsByName(" userId");
    for (i = 0; i < e.length; i++)
    {
    if (e[i].type == "text")
    {
    e[i].value = id;
    break;
    }
    }
    }
    }
    window.close();
    }

    </script>


  • CryingClinton

    #2
    Re: help please - script problem in IE 6 - (but not in Mozilla/Firefox)

    I am assuming that this function is contained in a new window opened by an
    existing one.

    I think the most possible problem comes from 'if (w && !w.closed) '

    In IE6, if w is undefined, statement 'if(w)' will case error, it is a little
    bit strange but it is true (- when w is null or equals to false, the
    statement is just fine).

    You can use "if(typeof(w)== 'object')" to replace the "if(w)". (use
    toLowerCase() if necessory, anyway IE always returns lowercased result for
    typeof() function)


    ccton

    --
    Great domain names provide SEO, branding, and a memorable experience for your users. Get a premium domain today.

    [color=blue]
    >
    > <script language="javas cript">
    >
    > function userSelected(id )
    > {
    > var w = window.opener;
    > if (w && !w.closed)
    > {
    > if (w.document)
    > {
    > var e = w.document.getE lementsByName(" userId");
    > for (i = 0; i < e.length; i++)
    > {
    > if (e[i].type == "text")
    > {
    > e[i].value = id;
    > break;
    > }
    > }
    > }
    > }
    > window.close();
    > }
    >
    > </script>
    >
    >[/color]


    Comment

    • Richard Cornford

      #3
      Re: help please - script problem in IE 6 - (but not in Mozilla/Firefox)

      CryingClinton wrote:
      <snip>[color=blue]
      > In IE6, if w is undefined, statement 'if(w)' will case error,
      > it is a little bit strange but it is true (- when w is null
      > or equals to false, the statement is just fine).[/color]

      An - if(w) - statement does not produce an error on IE6 (or any other
      ECMA 262 conforming language implementation) when - w - is undefined (in
      the sense of holding a value of - undefined -). That statement will
      produce a RefferenceError in the internal - GetValue - function
      whenever - w - is _undeclared_, but - w - is declared as a local
      variable in the posted code.
      [color=blue]
      > You can use "if(typeof(w)== 'object')" to replace the "if(w)".
      > (use toLowerCase() if necessory, anyway IE always returns
      > lowercased result for typeof() function)[/color]
      <snip>

      typeof is not a function it is a unary operator (ECMA 262 3rd edition,
      section 11.4.3). Parenthesising its operand is unnecessary and can be
      misleading, but is harmless in terms of the operation of the code. One
      byte fewer would need to be downloaded if the operand was separated from
      the - typeof - operator by only a space character and not parenthesised.

      Except when the operand is a host object, ECMA 262 requires the -
      typeof - operator to _always_ produce a string that is all lower case.
      [color=blue][color=green]
      >> <script language="javas cript">
      >>
      >> function userSelected(id )
      >> {
      >> var w = window.opener;[/color][/color]
      <snip>

      Please do not top-post on comp.lang.javas cript (see the FAQ).

      Richard.


      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: help please - script problem in IE 6 - (but not in Mozilla/Firefox)

        Derek wrote:[color=blue]
        > I have the following script in a page and it gets an error in IE 6.
        > Says something about an invalid argument but the line number doesn't
        > help since I can't see the javascript code when viewing source.[/color]

        If you are including script files, look in the specified line of all
        included script files. A bug in the error reporting feature of IE
        causes the filename of the document that includes the script files
        to be displayed instead of the included script file itself (where
        the scripting bug really is).
        [color=blue]
        > I have this script in a popup page where the user selects a user ID
        > (id) and that value is then placed into one of the parent window's
        > form text field called "userId". This script works fine in the
        > latest Mozilla and Mozilla Firefox but not in IE 6. Any help on why
        > this script might not work or a better way of accomplishing this task
        > would be appreciated.[/color]

        The precise error message would have helped even if you cannot find the
        script code it refers to.
        [color=blue]
        > [...]
        > <script language="javas cript">[/color]

        Replace with:

        <script type="text/javascript">
        [color=blue]
        > function userSelected(id )
        > {
        > var w = window.opener;
        > if (w && !w.closed)
        > {[/color]
        var d;
        [color=blue]
        > if (w.document)[/color]

        Replace with:

        if ((d = w.document))
        [color=blue]
        > {
        > var e = w.document.getE lementsByName(" userId");[/color]

        From the following code I must assume that the elements with name
        "userId" are form elements. Why do you not use the `elements'
        collection? It is as standards compliant as the getElementsByNa me()
        Method (both are defined in DOM Level 2, one in the Core and one in
        the DOM HTML Specification) *and* it is downwards compatible (DOM
        Level 0 as of NN/IE 3+).

        Replace with:

        var e = d.forms[0].elements['userId'];
        [color=blue]
        > for (i = 0; i < e.length; i++)[/color]
        ^^ ^^^^^^^^
        Avoid globals. Avoid repeated deep references.

        Replace with:

        for (var i = 0, len = e.length; i < len; i++)

        Make sure that there are more elements of that name because no
        collection of elements of that name would be formed otherwise
        (d.forms[0].elements['...'] would refer to a (HTML)Input(Ele ment)
        object instead).
        [color=blue]
        > {[/color]

        var o = e[i];
        [color=blue]
        > if (e[i].type == "text")
        > {
        > e[i].value = id; break;
        > }[/color]

        Replace with:

        if (o.type == "text")
        {
        o.value = id;
        break;
        }
        [color=blue]
        > }
        > }
        > }
        > window.close();
        > }[/color]

        The rest of the script (even the updated parts) looks syntactically OK.


        PointedEars

        Comment

        • CryingClinton

          #5
          Re: help please - script problem in IE 6 - (but not in Mozilla/Firefox)

          You made me nervous, man ! :)

          --
          Great domain names provide SEO, branding, and a memorable experience for your users. Get a premium domain today.

          "Richard Cornford" <Richard@litote s.demon.co.uk>
          [color=blue]
          > <snip>
          >
          > Please do not top-post on comp.lang.javas cript (see the FAQ).
          >
          > Richard.
          >
          >[/color]


          Comment

          Working...