Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > add() method

Reply
Thread Tools

add() method

 
 
Luca Berti
Guest
Posts: n/a
 
      08-13-2004
Someone can tell me why this code does everything but work under IE (6)???

var newEditor = new Option(document.add_editor.nome.value,
document.add_editor.nome.value);
opener.document.newsform.editor.options.add(newEdi tor, null);
opener.document.newsform.editor.selectedIndex =
opener.document.newsform.editor.length - 1;


 
Reply With Quote
 
 
 
 
kaeli
Guest
Posts: n/a
 
      08-13-2004
In article <411d0688$0$4890$>,
enlightened us with...
> Someone can tell me why this code does everything but work under IE (6)???
>
> var newEditor = new Option(document.add_editor.nome.value,
> document.add_editor.nome.value);
> opener.document.newsform.editor.options.add(newEdi tor, null);
> opener.document.newsform.editor.selectedIndex =
> opener.document.newsform.editor.length - 1;
>
>
>


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:
http://www.ipwebdesign.net/kaelisSpa...icSelects.html

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.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      08-14-2004
On Fri, 13 Aug 2004 20:20:54 +0200, Luca Berti <>
wrote:

> Someone can tell me why this code does everything but work under IE
> (6)???
>
> var newEditor = new Option(document.add_editor.nome.value,
> document.add_editor.nome.value);
> opener.document.newsform.editor.options.add(newEdi tor, null);
> opener.document.newsform.editor.selectedIndex =
> opener.document.newsform.editor.length - 1;


The problem here is that Microsoft, for some reason, implemented the DOM
method, HTMLSelectElement.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 HTMLSelectElement.options collection, simply assigning
where you want the option to go.

When you use the newer DOM approach with document.createElement(), you add
the resulting element using HTMLSelectElement.add().

So choose either:

var newEditor = new Option(
document.add_editor.nome.value,
document.add_editor.nome.value);
opener.document.newsform.editor.options[
opener.document.newsform.editor.length] = newEditor;

or:

var sel = opener.document.newsform.editor;

if(document.createElement && sel.add) {
var newEditor = document.createElement('OPTION');

if(newEditor) {
var e;

newEditor.text = newEditor.value =
document.add_editor.nome.value;

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

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
 
Reply With Quote
 
Luca Berti
Guest
Posts: n/a
 
      08-14-2004
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.

> 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.


Thx for the advce.
Luca


 
Reply With Quote
 
Steve van Dongen
Guest
Posts: n/a
 
      08-14-2004
"Luca Berti" <> wrote:

>Someone can tell me why this code does everything but work under IE (6)???
>
> var newEditor = new Option(document.add_editor.nome.value,
>document.add_editor.nome.value);
> opener.document.newsform.editor.options.add(newEdi tor, null);
> opener.document.newsform.editor.selectedIndex =
>opener.document.newsform.editor.length - 1;


PRB: Problem Adding New Option to Select Box in Another Frame in
Internet Explorer 5
http://support.microsoft.com/?id=237831

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
invoke a method by reflection£¬the method's parameters can not be ArrayList? jerry051 ASP .Net 2 08-02-2005 10:35 AM
BC30289: Statement cannot appear within a method body. End of method assumed. Carlos Oliveira ASP .Net 0 08-19-2004 07:51 PM
Difference between Delete method and RemoveRow method CW ASP .Net 0 04-01-2004 01:07 AM
ASP.NET: BC30289: Statement cannot appear within a method body. End of method assumed. Mike Wilmot ASP .Net 0 12-15-2003 07:49 PM



Advertisments