add() method

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

    add() method

    Someone can tell me why this code does everything but work under IE (6)???

    var newEditor = new Option(document .add_editor.nom e.value,
    document.add_ed itor.nome.value );
    opener.document .newsform.edito r.options.add(n ewEditor, null);
    opener.document .newsform.edito r.selectedIndex =
    opener.document .newsform.edito r.length - 1;


  • kaeli

    #2
    Re: add() method

    In article <411d0688$0$489 0$5402220f@news .sunrise.ch>, lucaberti@frees urf.ch
    enlightened us with...[color=blue]
    > Someone can tell me why this code does everything but work under IE (6)???
    >
    > var newEditor = new Option(document .add_editor.nom e.value,
    > document.add_ed itor.nome.value );
    > opener.document .newsform.edito r.options.add(n ewEditor, null);
    > opener.document .newsform.edito r.selectedIndex =
    > opener.document .newsform.edito r.length - 1;
    >
    >
    >[/color]

    Not really, since I don't have testable code here, just a fragment with your
    assertion that it doesn't "work". I have no idea what you mean by that. If
    you took your car to the mechanic, would you tell him that it doesn't work?
    Or would you tell him that when you do something, something else happens, and
    so on?
    How can I know if you properly defined your form elements, if this page even
    HAS an opener, and so on?

    Anyway, making new options and adding them somewhere...
    See my example here:


    If you want us to help you, you need to post something we can test or a URL
    to the complete code. Do NOT post a huge amount of code. Just a small example
    that demonstrates the problem in a way we can copy and test. And DO tell us
    what you expected to happen, what did happen, and any errors, if any show up.

    Half the time, trimming code to a small test version shows you what the error
    was, anyway.

    --
    --
    ~kaeli~
    Jesus saves, Allah protects, and Cthulhu thinks you'd make
    a nice sandwich.



    Comment

    • Michael Winter

      #3
      Re: add() method

      On Fri, 13 Aug 2004 20:20:54 +0200, Luca Berti <lucaberti@free surf.ch>
      wrote:
      [color=blue]
      > Someone can tell me why this code does everything but work under IE
      > (6)???
      >
      > var newEditor = new Option(document .add_editor.nom e.value,
      > document.add_ed itor.nome.value );
      > opener.document .newsform.edito r.options.add(n ewEditor, null);
      > opener.document .newsform.edito r.selectedIndex =
      > opener.document .newsform.edito r.length - 1;[/color]

      The problem here is that Microsoft, for some reason, implemented the DOM
      method, HTMLSelectEleme nt.add(), but changed its signature. Instead of the
      second argument being a required object reference (or null), it's an
      optional ordinal number.

      Unfortunately, this means having to cope with two completely incompatible
      methods. There are three ways to deal with this problem, but I wouldn't
      call any of them attractive:

      1) Detect IE and call the relevant method.
      2) Use try/catch, trying one set of parameters then the other.
      3) Use the older "new Option" approach for all cases.

      The first two options are unreliable. A lot of browsers disguise
      themselves as IE, so detecting IE with any certainty is impossible. The
      use of try/catch depends upon it existing in the first place; they are
      fairly recent commands, and there's no real way to check if they can be
      used, but this would be the best, otherwise.

      The third option, whilst useable, shouldn't (in my opinion) be used for
      anything other than a fallback approach for old browsers that don't
      support the DOM.

      Secondly, whilst this isn't a problem, I wouldn't advise it: mixing two
      approaches.

      When you use the older "new Option" approach, you add the resulting
      element using the HTMLSelectEleme nt.options collection, simply assigning
      where you want the option to go.

      When you use the newer DOM approach with document.create Element(), you add
      the resulting element using HTMLSelectEleme nt.add().

      So choose either:

      var newEditor = new Option(
      document.add_ed itor.nome.value ,
      document.add_ed itor.nome.value );
      opener.document .newsform.edito r.options[
      opener.document .newsform.edito r.length] = newEditor;

      or:

      var sel = opener.document .newsform.edito r;

      if(document.cre ateElement && sel.add) {
      var newEditor = document.create Element('OPTION ');

      if(newEditor) {
      var e;

      newEditor.text = newEditor.value =
      document.add_ed itor.nome.value ;

      try {
      sel.add(newEdit or, null);
      } catch(e) {
      sel.add(newEdit or);
      }
      }
      }

      By the way, you should look into saving object references, rather than
      accessing each element using its fully qualified reference. It's more
      efficient and makes for a smaller script. For example:

      var f = document.forms['formName'];
      var e = f.elements['controlName'];

      or

      var c = document.forms['formName'].elements;
      var e = c['controlName'];

      Which you choose depends upon whether you need to access properties of the
      form, or just its controls.

      Mike

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

      Comment

      • Luca Berti

        #4
        Re: add() method

        I implemented your code, but IE continued to traw out exeptions...
        Then I copied the code in a function on the page that contains the "SELECT"
        element and called that one from the opened window. Now it works.
        [color=blue]
        > By the way, you should look into saving object references, rather than
        > accessing each element using its fully qualified reference. It's more
        > efficient and makes for a smaller script.[/color]

        Thx for the advce.
        Luca


        Comment

        • Steve van Dongen

          #5
          Re: add() method

          "Luca Berti" <lucaberti@free surf.ch> wrote:
          [color=blue]
          >Someone can tell me why this code does everything but work under IE (6)???
          >
          > var newEditor = new Option(document .add_editor.nom e.value,
          >document.add_e ditor.nome.valu e);
          > opener.document .newsform.edito r.options.add(n ewEditor, null);
          > opener.document .newsform.edito r.selectedIndex =
          >opener.documen t.newsform.edit or.length - 1;[/color]

          PRB: Problem Adding New Option to Select Box in Another Frame in
          Internet Explorer 5
          Microsoft Support is here to help you with Microsoft products. Find how-to articles, videos, and training for Microsoft Copilot, Microsoft 365, Windows 11, Surface, and more.


          SYMPTOMS
          In Internet Explore 4.0 it is possible to create a new option in one
          frame and add it to a select box in another frame. This functionality
          is not supported in Internet Explorer 5.

          Using this functionality causes the following Java script error:
          Object does not support this property or method

          CAUSE
          This was not an intended functionality, which caused some instability.
          It was removed from Internet Explorer 5.

          RESOLUTION
          There are two ways to work around this. The first way is call Internet
          Explorer 5's new createElement method on the target frame. The second
          method is to create a subroutine in the target frame to create the
          option and then return a reference to it.

          STATUS
          This behavior is by design.

          Regards,
          Steve

          Comment

          Working...