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
  • why all this pain when you can be cross browser and cross platform in a flash using JQuery? for example: $('#myForm') should give you the form... in all browsers. – Davide Piras Commented Sep 15, 2011 at 19:24
  • Could we see the HTML?...Sounds like you have some invalid HTML. – John Hartsock Commented Sep 15, 2011 at 19:25
  • 1 @Davide Piras - you suggest he add tens of thousands of lines of javascript (jQuery) just to do what 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
  • @Davide: Right, but it's not a browser issue; I'm using the same browser, with the same HTML, same Javascript... and it errors out locally, but not on the server. I'm not concerned with how to fix it, it's trivial. I'm concerned with why it's happening in the first place. – voithos Commented Sep 15, 2011 at 19:57
Add a comment  | 

4 Answers 4

Reset to default 13

EDIT 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