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

D

DrKen

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
 
D

DrKen

Also, moving the script block before the <HEAD> tag does not change
the fact that the function runs twice.
 
E

Elegie

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

Jeff North

On Thu, 23 Jun 2011 14:04:08 -0700 (PDT), in comp.lang.javascript
DrKen <[email protected]>
| 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.
 
M

Mike Duffy

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

Evertjan.

Mike Duffy wrote on 24 jun 2011 in comp.lang.javascript:
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.
 
T

Thomas 'PointedEars' Lahn

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

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

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.

Should be qualified: window.alert(…);

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

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

Mike Duffy

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


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

Denis McMahon

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top