Recieving Error: 'theForm' is undefined

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I have an ASP.NET application which is giving the following JavaScript
error:

'theForm' is undefined

However, when I did a View Source I noticed that ASP.NET added the following
code:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>

I was told in a previous posting that the reason for the error is that the
script containing the declaration and assigning of a value to it occur
before the
 
If the page does not have a form named "form1", then the variable
declaration will fail and the variable "theForm" won't be able to be
created.


var theForm = document.forms['form1'];
 
either the script referencing theForm occurs before the declaration or
form1 is not defined (a form tag with name form1).


-- bruce (sqlwork.com)
 
And, remember "Form1" != "form1".



Scott M. said:
If the page does not have a form named "form1", then the variable
declaration will fail and the variable "theForm" won't be able to be
created.


var theForm = document.forms['form1'];


Nathan Sokalski said:
I have an ASP.NET application which is giving the following JavaScript
error:

'theForm' is undefined

However, when I did a View Source I noticed that ASP.NET added the
following code:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>

I was told in a previous posting that the reason for the error is that
the script containing the declaration and assigning of a value to it
occur before the
 
I have an ASP.NET application which is giving the following JavaScript
error:

'theForm' is undefined

However, when I did a View Source I noticed that ASP.NET added the following
code:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;}

function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}}

// -->
</script>

I was told in a previous posting that the reason for the error is that the
script containing the declaration and assigning of a value to it occur
before the

try

var theForm = document.getElementById("form1");
 
He can't try that, Alexei, because the code is being injected by ASP.NET.

He can't change the embedded javascript funtions, if he's using ASP.NET 2.0.
There's a chance he mioght be able to do that, if he's using ASP.NET 1.1.

All he'd have to do is edit the scripts located in
\wwwroot\aspnet_client\system_web\1_1_4322.

The problem doesn't seem to be that the function is buggy, though.

If Nathan were to rename the page's form to "form2",
instead of naming it "form1", he should see this :

<script type="text/javascript">
<!--
var theForm = document.forms['form2'];
if (!theForm) {
theForm = document.form2;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>

Notice that the source reveals that the script has picked up the form's new name.

If that doesn't show up, and the error persists, it's a timing problem.
The javascript is running before the form has been rendered.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
Alexey Smirnov said:
I have an ASP.NET application which is giving the following JavaScript
error:

'theForm' is undefined

However, when I did a View Source I noticed that ASP.NET added the following
code:

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;}

function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}}

// -->
</script>

I was told in a previous posting that the reason for the error is that the
script containing the declaration and assigning of a value to it occur
before the

try

var theForm = document.getElementById("form1");
 
Back
Top