On Sep 13, 8:36 am, Gregor Kofler wrote:
james_027 meinte:
if the var invoice_items = new Array(); is commented it won't
work in IE6 saying "the object doesn't support this property
or method" but when it is uncommented it works.
In this case invoice_item becomes a *global* variable.
var invoice_items = [...] will work.
To which it would be possible to add an explanation of why that might
make a difference on IE and not necessarily matter on other browsers.
It will be because the HTML contains an element with an ID attribute
set to "invoice_item" and when that is the case IE adds a reference to
the DOM element as a named property of the global object under the
name "invoice_item" and makes that property read only. Thus the array
does not get assigned to the property of the global object when -
invoice_items = [...]; - is executed, and subsequent attempts to call
array methods on an object that remains an Element result in the
(accurate) error "object doesn't support this property or method".
There are two ways of avoiding this issue. The first is, as suggested,
declaring the - invoice_items - in the functions so it masks the
property of the global object. The other is to explicitly declare -
invoice_items - as a global variable. If that is done IE does not use
the existence of an element in the DOM with a corresponding ID
attribute as an excuse for creating a property of the global object,
and so the resulting declared global variable has no DOM Element
reference assigned and it remains read/write.
The whole issue is avoided by following the 'best practice' advice of
always explicitly declaring all variables.