faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

84 of 95 people (88%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Why does my IE form (processing) code not work with NN?
Why does my IE form (processing) code not work with NN?

Feb 16th, 2000 16:54
Martin Honnen,


There are (at least) three common errors. 
1) IE reflects named FORMs (and 
other IDed elements) as properties of the window object so with IE
  <FORM NAME="aForm">
results in the js object
  window.aForm
or
  aForm
as the window is the global object. NN however reflects the named FORMs 
as properties of the document object so in NN (and in IE as fortunately 
IE does that too) the above HTML results in the js object
  document.aForm
So to script FORMs cross browser use
  window.document.formName
or
  document.formName
NOT
  formName
or
  window.formName

2) Besides the wrong FORM referencing another common error is 
referencing the value of a SELECT element. While IE provides
  document.formName.selectName.value
NN (2/3/4) doesn't provide that property but you have to index the 
options array as follows
  var sel = document.formName.selectName;
  var value = sel.options[sel.selectedIndex].value;
to get the currently selected value. IE supports this way too so to 
write cross browser form processing code use this way for SELECT 
elements.

3) Last but not least if you put your FORM in a positioned element/in a 
layer NN4 requires special coding to reference the FORM:
  window.document.layerName.document.formName
as positioned elements/layers have their own document. This reference 
might even be longer if you nest your positioned elements as then you 
need to nest the
  window.document.layerName.document.layer2Name.document.formName
as well.
Note that form references in IE4/5 and NN6 don't change when forms are 
contained in positioned elements so there you still use
  document.formName