Why would a function be executed twice if only called onload()?

Discussion in 'Javascript' started by DrKen, Jun 23, 2011.

  1. DrKen

    DrKen Guest

    I have this body tag:
    <body id="page-aboutme" onload="executeOmniture();" >

    I have this script block, which contains one function, at the bottom
    of my HTML:
    <script type="text/javascript">
    // <!--Code to support Omniture -->
    // Added for SSD-245
    var loopCount = 1;
    function executeOmniture()
    {
    omniIdentifier = document.getElementById("omniEmail").value;

    //<![CDATA[
    alert("loopCount:" + loopCount);
    loopCount = loopCount + 1;
    s.channel="Admissions";
    s.pageName="Applications: Graduate: Start";
    s.hier1="applications,graduate";
    alert("omniIdentifier: " + omniIdentifier);
    s.events="event8:GRAD"+ "TEST" + omniIdentifier;
    //]]>
    }
    </script>

    </body>
    </html>

    I put the counter in because I was seeing the last alert() statement
    run twice. It appears to be run at page load and after the submit()
    button is clicked! All this code is in a function and only called
    once as specified in the body tag. It is only found in the entire
    file in those two places. A function should only run when called,
    right? There should be only one load event, right? Why/how could the
    function be executed twice? That makes no sense to me at all.
    Thanks.


    Ken
    DrKen, Jun 23, 2011
    #1
    1. Advertising

  2. DrKen

    DrKen Guest

    Also, moving the script block before the <HEAD> tag does not change
    the fact that the function runs twice.
    DrKen, Jun 23, 2011
    #2
    1. Advertising

  3. DrKen

    Elegie Guest

    On 23/06/2011 23:04, DrKen wrote :

    Hi,

    > I put the counter in because I was seeing the last alert() statement
    > run twice. It appears to be run at page load and after the submit()
    > button is clicked! All this code is in a function and only called
    > once as specified in the body tag. It is only found in the entire
    > file in those two places. A function should only run when called,
    > right?


    Yes, absolutely.

    > There should be only one load event, right?


    Yes, but there may be different listeners attached to an event. This
    means an event can trigger many functions at once, each function may
    also call other functions as well... Have you checked your javascript
    includes?

    > Why/how could the
    > function be executed twice? That makes no sense to me at all.


    If this is not a listener issue, then maybe the form posts to the same
    page ('action="#"'). The page would then be served again by the server
    when the form is submitted (the function being called again) - this is a
    classical pattern in dynamic web applications (for instance to show form
    validation errors).

    HTH,
    Elegie.
    Elegie, Jun 23, 2011
    #3
  4. DrKen

    Jeff North Guest

    On Thu, 23 Jun 2011 14:04:08 -0700 (PDT), in comp.lang.javascript
    DrKen <>
    <>
    wrote:

    >| I have this body tag:
    >| <body id="page-aboutme" onload="executeOmniture();" >
    >|
    >| I have this script block, which contains one function, at the bottom
    >| of my HTML:
    >| <script type="text/javascript">
    >| // <!--Code to support Omniture -->
    >| // Added for SSD-245
    >| var loopCount = 1;
    >| function executeOmniture()
    >| {
    >| omniIdentifier = document.getElementById("omniEmail").value;
    >|
    >| //<![CDATA[
    >| alert("loopCount:" + loopCount);
    >| loopCount = loopCount + 1;
    >| s.channel="Admissions";
    >| s.pageName="Applications: Graduate: Start";
    >| s.hier1="applications,graduate";
    >| alert("omniIdentifier: " + omniIdentifier);
    >| s.events="event8:GRAD"+ "TEST" + omniIdentifier;
    >| //]]>
    >| }
    >| </script>
    >|
    >| </body>
    >| </html>
    >|
    >| I put the counter in because I was seeing the last alert() statement
    >| run twice. It appears to be run at page load and after the submit()
    >| button is clicked!


    If the form doesn't have an action attribute then the form submits to
    the current page. This will cause the alert to be displayed again as
    the page is reloaded.

    >| All this code is in a function and only called
    >| once as specified in the body tag. It is only found in the entire
    >| file in those two places. A function should only run when called,
    >| right? There should be only one load event, right? Why/how could the
    >| function be executed twice? That makes no sense to me at all.
    >| Thanks.
    >|
    >|
    >| Ken
    Jeff North, Jun 24, 2011
    #4
  5. DrKen

    Mike Duffy Guest

    Jeff North <> wrote in
    news::

    > If the form doesn't have an action attribute then the form submits
    > to the current page. This will cause the alert to be displayed
    > again as the page is reloaded.


    If the action is set to a null string, is that the same as not having
    one? i.e.:

    form name="foo" action=""

    What should I do if I DON'T want <Enter> to submit the form?

    --
    http://pages.videotron.com/duffym/index.htm#usenet?
    Mike Duffy, Jun 24, 2011
    #5
  6. DrKen

    Evertjan. Guest

    Mike Duffy wrote on 24 jun 2011 in comp.lang.javascript:

    > Jeff North <> wrote in
    > news::
    >
    >> If the form doesn't have an action attribute then the form submits
    >> to the current page. This will cause the alert to be displayed
    >> again as the page is reloaded.

    >
    > If the action is set to a null string, is that the same as not having
    > one? i.e.:
    >
    > form name="foo" action=""
    >
    > What should I do if I DON'T want <Enter> to submit the form?


    <form onsubmit='return false;'>
    <input name='do' value='done'>
    <input type='button'
    onclick='this.form.submit();' value='submit'>
    </form>

    do not use type='submit' here.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jun 24, 2011
    #6
  7. Jeff North wrote:

    > DrKen wrote:
    >>| I have this body tag:
    >>| <body id="page-aboutme" onload="executeOmniture();" >
    >>|
    >>| I have this script block, which contains one function, at the bottom
    >>| of my HTML:
    >>| <script type="text/javascript">
    >>| // <!--Code to support Omniture -->


    How borken must a piece of software be not to properly deal with (like:
    ignore) `script' element content on today's Web (HTML 3.2 [0] + 14.5 years,
    HTML 4.01 + 11.5 years)?

    >>| // Added for SSD-245
    >>| var loopCount = 1;
    >>| function executeOmniture()
    >>| {
    >>| omniIdentifier = document.getElementById("omniEmail").value;


    The identifier left-hand side is not declared in the posted code.

    >>| //<![CDATA[


    One should know whether one uses XHTML (where this declaration is necessary,
    for markup validation, or in anticipation of future user agents parsing
    XHTML as such even without the specific Content-Type header field value), or
    HTML (where it is not necessary).

    Besides, there is no `<' or `&' in the script code, so the CDATA declaration
    is superfluous in any case.

    >>| alert("loopCount:" + loopCount);


    Should be qualified: window.alert(…);

    >>| loopCount = loopCount + 1;
    >>| s.channel="Admissions";


    `s' is undefined (and undeclared) in the posted code.

    >>| s.pageName="Applications: Graduate: Start";
    >>| s.hier1="applications,graduate";
    >>| alert("omniIdentifier: " + omniIdentifier);


    See above. Further, this should not be used in that context for production
    code.

    >>| s.events="event8:GRAD"+ "TEST" + omniIdentifier;
    >>| //]]>
    >>| }
    >>| </script>
    >>|
    >>| </body>
    >>| </html>
    >>|
    >>| I put the counter in because I was seeing the last alert() statement
    >>| run twice. It appears to be run at page load and after the submit()
    >>| button is clicked!

    >
    > If the form doesn't have an action attribute then the form submits to
    > the current page.


    Not necessarily. The `action' attribute of FORM elements is #REQUIRED in
    HTML 4.01 (not specifying it is a syntax error). An initial value for that
    attribute is (therefore) not specified [1]. It is only since HTML5 that the
    notion is introduced that the `action' attribute specification can be
    omitted, in which case it is specified to default to the empty string.
    RFC 3986, section 4.4, specifies the empty string to be a same-document
    reference [3], but browsers are known to not always honor this standard.
    Further, we do not know whether which version of HTML the OP uses, nor do we
    know their testing environment.

    > This will cause the alert to be displayed again as the page is reloaded.


    In order to be sure about that, we will need to see the form, and be told
    about the employed HTML version and testing environment.


    PointedEars
    ___________
    [0] <http://www.w3.org/TR/REC-html32#script>
    [1] <http://www.w3.org/TR/html401/interact/forms.html#edef-FORM>
    [2] <http://www.w3.org/TR/html5/association-of-controls-and-forms.html#attr-
    fs-action>
    [3] <http://tools.ietf.org/html/rfc3986>
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
    Thomas 'PointedEars' Lahn, Jun 24, 2011
    #7
  8. DrKen

    Mike Duffy Guest

    "Evertjan." <> wrote in
    news:Xns9F0E680B23CD4eejj99@194.109.133.242:

    > Mike Duffy wrote on 24 jun 2011 in comp.lang.javascript:
    >
    >> Jeff North <> wrote in
    >> news::
    >>
    >>> If the form doesn't have an action attribute then the form
    >>> submits to the current page. This will cause the alert to be
    >>> displayed again as the page is reloaded.

    >>
    >> If the action is set to a null string, is that the same as not
    >> having one? i.e.:
    >>
    >> form name="foo" action=""
    >>
    >> What should I do if I DON'T want <Enter> to submit the form?

    >
    > <form onsubmit='return false;'>
    > <input name='do' value='done'>
    > <input type='button'
    > onclick='this.form.submit();' value='submit'>
    > </form>
    >
    > do not use type='submit' here.


    Thank you! I wanted to pop-up an alert ("Please use the button to
    send the form") when someone typed Enter, but I could not see how to
    do it other than by putting a Enter-keystroke detector on every input
    field.

    I will try your suggestion.

    --
    http://pages.videotron.com/duffym/index.htm
    Mike Duffy, Jun 24, 2011
    #8
  9. On Thu, 23 Jun 2011 14:04:08 -0700, DrKen wrote:

    > A function should only run when called, right? There
    > should be only one load event, right? Why/how could the function be
    > executed twice? That makes no sense to me at all. Thanks.


    Given the following:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type"
    content="text/html; charset=us-ascii">
    <meta name="MSSmartTagsPreventParsing" content="TRUE">
    <title>Onload</title>
    </head>
    <body onload="alert('Loaded')">
    <form>
    <p>
    <input type="submit" value="submit">
    </p>
    </form>
    </body>
    </html>

    Every time the submit button in the form is clicked, a page reload occurs
    because with no method and action defined to the fine, the default is
    method is "get" and the default action is "the current page", so every
    "submit" causes a page reload.

    Rgds

    Denis McMahon

    (I thought I posted this yesterday, but i can't see it in the group!)
    Denis McMahon, Jun 24, 2011
    #9
    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. Fabio R.
    Replies:
    0
    Views:
    792
    Fabio R.
    Mar 25, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  3. Bob
    Replies:
    10
    Views:
    840
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Feb 23, 2007
  4. TechCrazy
    Replies:
    5
    Views:
    397
    Karl Heinz Buchegger
    Jul 15, 2005
  5. Replies:
    5
    Views:
    255
    Thomas 'PointedEars' Lahn
    May 15, 2005
Loading...

Share This Page