Recieving Error "'theForm' is undefined" In ASP.NET Application Even With The Line "var theForm = do

Discussion in 'ASP .Net' started by Nathan Sokalski, Jun 10, 2007.

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

    'theForm' is undefined

    However, when I do a View Source one of the <script> elements is as follows:

    <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>

    You will notice that the first line of code in this script element creates
    and assigns a value to a variable named "theForm". This code was generated
    by ASP.NET, so it SHOULD be correct, and it looks to me like 'theForm' is
    defined. I have seen other people post this error before also, but I can't
    remember what they said they did to fix it. Any ideas? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Jun 10, 2007
    #1
    1. Advertising

  2. Nathan Sokalski

    Randy Webb Guest

    Re: Recieving Error "'theForm' is undefined" In ASP.NET ApplicationEven With The Line "var theForm = document.forms['form1'];"

    Nathan Sokalski said the following on 6/9/2007 8:57 PM:
    > I have an ASP.NET application which is giving the following JavaScript
    > error:
    >
    > 'theForm' is undefined
    >
    > However, when I do a View Source one of the <script> elements is as follows:
    >
    > <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>
    >
    > You will notice that the first line of code in this script element creates
    > and assigns a value to a variable named "theForm". This code was generated
    > by ASP.NET, so it SHOULD be correct,


    But it isn't "correct". The error is caused because the script block
    will be before the form element appears in the page and so it will be
    undefined because it doesn't exist yet.

    > and it looks to me like 'theForm' is defined.


    If, and only if, a form with the name/id attribute of "form1" exists at
    the time that the script executes.

    > I have seen other people post this error before also, but I can't
    > remember what they said they did to fix it. Any ideas? Thanks.


    1) Make sure the script block appears after the element in the page.
    2) Better, is to include it in the postback function. Failing that, wrap
    it in a function and call it via window.onload

    The entire if part that is not in a function is a bad way of writing a
    script though. I would be interested to see a scenario where theForm
    didn't get defined on the first line but got defined on the second.
    Meaning, a browser that didn't support document.forms['form1'] but did
    support document.form1

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jun 10, 2007
    #2
    1. Advertising

  3. Thank you for that information, it will definitely be useful at some point.
    However, it brings me to another problem:

    Because I did not specifically request that this script be added (it gets
    added automatically, my code does not include anything that says where to
    put the script or even that it should be included at all), what should I do
    to fix the problem? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Randy Webb" <> wrote in message
    news:...
    > Nathan Sokalski said the following on 6/9/2007 8:57 PM:
    >> I have an ASP.NET application which is giving the following JavaScript
    >> error:
    >>
    >> 'theForm' is undefined
    >>
    >> However, when I do a View Source one of the <script> elements is as
    >> follows:
    >>
    >> <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>
    >>
    >> You will notice that the first line of code in this script element
    >> creates and assigns a value to a variable named "theForm". This code was
    >> generated by ASP.NET, so it SHOULD be correct,

    >
    > But it isn't "correct". The error is caused because the script block will
    > be before the form element appears in the page and so it will be undefined
    > because it doesn't exist yet.
    >
    >> and it looks to me like 'theForm' is defined.

    >
    > If, and only if, a form with the name/id attribute of "form1" exists at
    > the time that the script executes.
    >
    >> I have seen other people post this error before also, but I can't
    >> remember what they said they did to fix it. Any ideas? Thanks.

    >
    > 1) Make sure the script block appears after the element in the page.
    > 2) Better, is to include it in the postback function. Failing that, wrap
    > it in a function and call it via window.onload
    >
    > The entire if part that is not in a function is a bad way of writing a
    > script though. I would be interested to see a scenario where theForm
    > didn't get defined on the first line but got defined on the second.
    > Meaning, a browser that didn't support document.forms['form1'] but did
    > support document.form1
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    > Javascript Best Practices -
    > http://www.JavascriptToolbox.com/bestpractices/
    Nathan Sokalski, Jun 10, 2007
    #3
  4. Nathan Sokalski

    Randy Webb Guest

    Re: Recieving Error "'theForm' is undefined" In ASP.NET ApplicationEven With The Line "var theForm = document.forms['form1'];"

    Nathan Sokalski said the following on 6/9/2007 9:55 PM:
    > Thank you for that information, it will definitely be useful at some point.
    > However, it brings me to another problem:
    >
    > Because I did not specifically request that this script be added (it gets
    > added automatically, my code does not include anything that says where to
    > put the script or even that it should be included at all), what should I do
    > to fix the problem? Thanks.


    Stop using ASP.NET :)

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jun 10, 2007
    #4
  5. Make sure you have something like
    <form id="form1" runat="server">
    on your form. You didn't rename the form1 did you?

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Nathan Sokalski" <> wrote in message
    news:eQEv$...
    >I have an ASP.NET application which is giving the following JavaScript
    >error:
    >
    > 'theForm' is undefined
    >
    > However, when I do a View Source one of the <script> elements is as
    > follows:
    >
    > <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>
    >
    > You will notice that the first line of code in this script element creates
    > and assigns a value to a variable named "theForm". This code was generated
    > by ASP.NET, so it SHOULD be correct, and it looks to me like 'theForm' is
    > defined. I have seen other people post this error before also, but I can't
    > remember what they said they did to fix it. Any ideas? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Eliyahu Goldin, Jun 10, 2007
    #5
  6. No, I did not rename my the form. The exact line of code copy & pasted from
    Visual Studio 2005 is:

    <form id="form1" runat="server">

    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Eliyahu Goldin" <> wrote in
    message news:%...
    > Make sure you have something like
    > <form id="form1" runat="server">
    > on your form. You didn't rename the form1 did you?
    >
    > --
    > Eliyahu Goldin,
    > Software Developer & Consultant
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "Nathan Sokalski" <> wrote in message
    > news:eQEv$...
    >>I have an ASP.NET application which is giving the following JavaScript
    >>error:
    >>
    >> 'theForm' is undefined
    >>
    >> However, when I do a View Source one of the <script> elements is as
    >> follows:
    >>
    >> <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>
    >>
    >> You will notice that the first line of code in this script element
    >> creates and assigns a value to a variable named "theForm". This code was
    >> generated by ASP.NET, so it SHOULD be correct, and it looks to me like
    >> 'theForm' is defined. I have seen other people post this error before
    >> also, but I can't remember what they said they did to fix it. Any ideas?
    >> Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    Nathan Sokalski, Jun 10, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stan Goodman

    Even older fart, even newer newbie

    Stan Goodman, Jul 3, 2003, in forum: Java
    Replies:
    11
    Views:
    686
    Stan Goodman
    Jul 4, 2003
  2. Mitch
    Replies:
    13
    Views:
    1,223
    Mitch
    Mar 10, 2006
  3. Nathan Sokalski

    Recieving Error: 'theForm' is undefined

    Nathan Sokalski, Jun 11, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    6,575
    Alexey Smirnov
    Jun 11, 2007
  4. chandra kantha via .NET 247

    .net recieving null object while accessing axis message style web service

    chandra kantha via .NET 247, Mar 29, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    248
    chandra kantha via .NET 247
    Mar 29, 2005
  5. Ammar

    Recieving mail/ASP

    Ammar, Jun 18, 2004, in forum: ASP General
    Replies:
    13
    Views:
    269
    Jeff Cochran
    Jun 20, 2004
Loading...

Share This Page