PDA

View Full Version : Problem with 'var x = new Object();' when 2 forms on the same page


wolfverine
12-31-2002, 03:30 PM
Hello,

Perhaps you've had the same problem as I.
Instruction : 'var OrderByData = new Object();' Failed to works when you have two forms on the page.
int he bloc :
<Script language="JavaScript">
alert('new Ojbect() OK ?');
var OrderByData = new Object();
alert('Yes!');
ORDERBY_Init(OrderByData,document.OrderBy.Order_By);
</SCRIPT>

=========================================
here is page with two forms :
<html>
<head></head>
<body>
<form method="Post" name="Object" id="Object" action="">
From 1
</Form>
<form name="OrderBy" method="Post" id="OrderBy">
Form 2
<Script language="JavaScript">
alert('new Ojbect() OK ?');
var OrderByData = new Object();
alert('Yes!');
ORDERBY_Init(OrderByData,document.OrderBy.Order_By);
</SCRIPT></form>
</body>
</html>
You've got Message : new Ojbect() OK ?
but not 'Yes!'

==============================================
here is page with one forms :
<html>
<head></head>
<body>
<Xform method="Post" name="Object" id="Object" action="">
Form 1 : Not a Form
</XForm>
<form name="OrderBy" method="Post" id="OrderBy">
Form 2
<Script language="JavaScript">
alert('new Ojbect() OK ?');
var OrderByData = new Object();
alert('Yes!');
ORDERBY_Init(OrderByData,document.OrderBy.Order_By);
</SCRIPT></form>
</body>
</html>

which is the same as Classes.htm except : (first form)
<form method="Post" name="Object" id="Object" action="">
which becomes
<Xform method="Post" name="Object" id="Object" action="">

AND

</Form>
which becomes
</XForm>

You've got both Message : new Ojbect() OK ?
and 'Yes!'

What prevent the Object to be created ??? when you've got two forms ?
When I try to create an Array it's works even when two forms ?

I've tryed this on IE 5.5 SP2

mordred
12-31-2002, 08:45 PM
You just stumbled over IE's shortcut snytax woes. A form element of the HTML page could be reached by IE's DOM like this (suppose it's id/name is "foo"):

document.foo.elements[0] // no problem here

document.forms['foo'].elements[0] // no problem here

foo.elements[0] // aha - that's the culprit.


So when you say in your code:

alert('new Ojbect() OK ?');
var OrderByData = new Object();
alert('Yes!');

and you unfortunately named your form "Object", IE is confused and tries to instantiate a new form element object, which is not correct.

Solution: Rename your form something different than a general name or keyword of JavaScript. Try "ObjectX" for example. It's inviting problems if you're naming certain HTML elements in IE like "Object" or "Image". Other browsers with a more stricter DOM don't have these admittedly annoying problems.

wolfverine
01-06-2003, 11:06 AM
FYI : The answer above solve my problem.

Thanks.