I recently ran into a problem: some JavaScript code that was working on other browsers (Google Chrome & Internet Explorer) consistently failed when I attempted to use Mozilla Firefox.
In fact, Firefox did absolutely nothing. A loading dialog was meant to appear while things were being processed in the background, and then it was meant to redirect to a new URL. Firefox just decided to refresh the current page instead. Not very helpful!
Looking at the Error Console located under the "Tools" menu, I soon realised that the browser couldn't find my form. For some reason, every other browser saw it, but Firefox thought that it didn't exist. I was stumped.
After spending time searching the Internet for answers, I finally stumbled upon the one I was looking for… I made the necessary changes, and Firefox began working!
What was the problem?
I was accessing a field in the HTML form using code like this:
alert( "Field value: " + myForm.FieldName.value );
But thanks to Bruce Barker, I solved it with something like this:
document.myForm.FieldName.value
The only change that was necessary was to include the 'document' namespace when referring to the form. Both Google Chrome and Internet Explorer didn't need that, and intelligently guessed which form I was referring to.
According to what Bruce posted on the forum that I linked to above, and for future reference:
Accessing a form using Javascript | |
---|---|
Any browser that supports JavaScript | document.myForm document.forms['myForm'] document.forms[0] |
Most modern browsers | document.getElementsByName('myForm')[0] document.getElementById('myForm') |
Google Chrome & Internet Explorer only | myForm document.all('myForm') |
Notes:
- If you see a number instead of the form name, that refers to the index number of the form. If you only have one form on the page it will always be at index 0.
- When using 'getElementById' you must give the form an ID as well.
P.S. This post was created with STE (a Wikidot Editor).
Image: Copyright © Mozilla Foundation and Mozilla Corporation
Interesting. I actually never knew that. I've always prepended the document anyway, and I don't remember where I learned to do that. Now I know to never exclude it (:
Timothy Foster - @tfAuroratide
Auroratide.com - Go here if you're nerdy like me
Glad you learnt something new from this :)
It was a surprise to me, so I posted this in case others weren't aware about the subtle difference with Firefox either.
~ Leiger - Wikidot Community Admin - Volunteer
Wikidot: Official Documentation | Wikidot Discord server | NEW: Wikiroo, made for Wikidot users (available early 2023)
I haven't used document.forms[n] for ages… I've always stuck with document.getElementById("id") - so I've never encountered the problem :D
Kenneth Tsang (@jxeeno)
I like clear, readable code … and therefore I was using the simplest possible method I could: typing only the name of the form. Turns out that didn't work so well ;-)
Probably my best option would be to store a reference to the form in a variable, then I can make it as simple as possible:
But I'm not doing that at the moment :S
Your solution to give everything an ID would work as well.
~ Leiger - Wikidot Community Admin - Volunteer
Wikidot: Official Documentation | Wikidot Discord server | NEW: Wikiroo, made for Wikidot users (available early 2023)
Post preview:
Close preview