admin管理员组文章数量:1186460
I've got an ASP.NET site running locally on my XP system using IIS Express. There is a live version of the site running on a Windows server.
In the web pages, often the Javascript will reference a form on the page using the style document.formName
, where formName
is the name of the form. As far as I know, this is a cross-browser method, along with document.forms.formName
and document.forms[0]
and so forth.
On my local development site, the reference document.frm1
(I know, bad naming practice) errors out; it is undefined. On the other hand, document.forms.frm1
works fine. Strangely, this doesn't occur on the server, although both pages are identical as far as the code goes. I've double checked with Firebug, and in both IE8 and Firefox 6.
Another weird part: checking with Firebug, document.frm1
is undefined, but document.frmClose
(another form) exists! Huh?!
Anyone experienced this before?
I've got an ASP.NET site running locally on my XP system using IIS Express. There is a live version of the site running on a Windows server.
In the web pages, often the Javascript will reference a form on the page using the style document.formName
, where formName
is the name of the form. As far as I know, this is a cross-browser method, along with document.forms.formName
and document.forms[0]
and so forth.
On my local development site, the reference document.frm1
(I know, bad naming practice) errors out; it is undefined. On the other hand, document.forms.frm1
works fine. Strangely, this doesn't occur on the server, although both pages are identical as far as the code goes. I've double checked with Firebug, and in both IE8 and Firefox 6.
Another weird part: checking with Firebug, document.frm1
is undefined, but document.frmClose
(another form) exists! Huh?!
Anyone experienced this before?
Share Improve this question edited Feb 11, 2013 at 0:14 John Saunders 162k26 gold badges250 silver badges402 bronze badges asked Sep 15, 2011 at 19:21 voithosvoithos 70.6k12 gold badges106 silver badges120 bronze badges 4 |4 Answers
Reset to default 13EDIT 1
I did some experimentation, and found that the id
property is used for document.forms
, whereas the name
property seems to be used for document.formName
http://jsfiddle.net/GVjsv/
Original Answer
Ensure that your javascript is not executing before DOM is ready. One way to help is to put your javascript at the bottom of the page, or if you're using a framework be sure to wrap the code in a ready
-type function:
jQuery: http://api.jquery.com/ready/
Mootools: http://mootools.net/docs/core/Utilities/DomReady
Vanilla javascript:
window.onload = function() {
// Code to be run.
}
The reason that this is inconsistent between servers could be that the local development server loads the page more quickly than the live server does. The timing works out so that you usually don't get the same error in both places - the emphasis is added because if you tried enough times, you would probably be able to reproduce the error in both places.
To get the name attribute added to your form element you need to add...
<system.web>
<pages controlRenderingCompatibilityVersion="3.5" /> <!-- http://www.asp.net/whitepapers/aspnet4/breaking-changes -->
</system.web>
...to your web.config
I recommend not using the name attribute for JavaScript identification purposes. Instead, give the element an id and use
document.getElementById("elementId");
e.g.
<form id="form1"></form>
<script>
var form = document.getElementById("form1");
</script>
The best solution for this is below:
Just add a name property in form tag
If you are using below code
<form id="form1"></form>
<script>
var form = document.form1; //getting undefined error here
</script>
What you need to perform here is:
add "name" property in form tag like below
<form id="form1" name="form1"></form>
<script>
var form = document.form1; //Now it will work fine and provide the correct value
</script>
本文标签: javascriptquotdocumentformNamequot is undefinedStack Overflow
版权声明:本文标题:javascript - "document.formName" is undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738315646a2074265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
document.getElementById
already does? Blindly adding a framework to your project just for the parts that duplicate standard functionality is Considered Harmful. – Chris Baker Commented Sep 15, 2011 at 19:46