[Javascript] ending after for() in firefox
Paul Novitski
paul at novitskisoftware.com
Mon Mar 6 09:22:29 CST 2006
At 06:10 AM 3/6/2006, Michael Borchers wrote:
>this script works fine in IE, the alerts after the for() are shown
>and even the new lines are created, but firefox seems to stop
>right after the for() in countRows().
>
>what's wrong?
Michael,
Here's your bug:
HTML:
<input type="button" value="Optionen hinzufügen"
onClick="addOptionsNumRows();" />
<input type="submit" name="insert" value="Speichern" />
JavaScript:
if(inputFields[i].getAttribute("name").substring(0,8) == "options[")
The if-test assumes that every input field has a "name" attribute
(your first button doesn't) and that the name will be at least 8
characters ("insert" is only 6).
When your script reaches your first button (value="Optionen
hinzufügen") it attempts to execute the substring() method on a
non-existent string object and fails.
For more robust code, and for ease of debugging, I like to break up
those long expressions into their component parts. For one thing, it
lets you insert alert() statements into incremental bits of logic to
pinpoint exactly where your script fails.
for(i=0;i<numTag;i++)
{
sName = inputFields[i].getAttribute("name");
if (sName) sName = sName.substring(0,8);
if (sName && sName == "options[")
{
...
}
}
or:
if (inputFields[i].getAttribute("name")
&& inputFields[i].getAttribute("name").length >= 8
&& inputFields[i].getAttribute("name").substring(0,8) == "options[")
{
...
}
(I can't recall if the substring() method will abort JavaScript if
operating on too short a string, but it doesn't hurt to play it safe
if you're not sure.)
Regards,
Paul
More information about the Javascript
mailing list