By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,563 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,563 IT Pros & Developers. It's quick & easy.

Location and Execution of JavaScript code

P: n/a
http://www.w3schools.com/js/js_whereto.asp

This link is to a JavaScript tutorial on w3schools. The page says that
a script put in the HEAD is executed only when called, whereas one put
in the BODY is executed as the page loads (implying invariably, whether
called or not).

To test, I tried the following two pages:
<HTML>
<HEAD>
<SCRIPT>
<!--
document.write("I am a statement and I reside in the HEAD tag of
this page.")
-->
</SCRIPT>
</HEAD>

<BODY>
<SCRIPT>
<!--
document.write("I am a statement and I reside in the BODY tag of
this page.")
-->
</SCRIPT>
</BODY>
</HTML>




Both of them executed. So, I thought the statement made in that
tutorial must pertain to functions declared in JavaScript. To test it,
I tried another file:
<HTML>

<HEAD>
<SCRIPT langauge="JavaScript" type="text/javascript">
<!--
void function foo()
{
document.write("I'm function foo() and I reside in the HEAD tag of
this page.");
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<SCRIPT langauge="JavaScript" type="text/javascript">
<!--
void function bar()
{
document.write("I am function bar() and I reside in the BODY tag of
this page.");
}
//-->
</SCRIPT>
</BODY>

</HTML>
In this, none of the functions exectued. Then I called each one of them
and they executed, implying the following as per my tests:
1. If a snippet of JavaScript code is written outside a function, and
is not tied to the event of an HTML control, it will execute
invariably. The time it will execute will be determined on its sequence
in the file and it will be executed in the sequence the page loads. It
doesn't matter where the snippet is put -- the HEAD or the BODY tag;

and

2. If a snippet of JavaScript code is embedded inside a function, it
will not be executed, no matter where it is put -- HEAD or BODY, unless
it is explicitly called or triggered by the occurance of the event it
is tied to.

Please reconcile the learning I got from my test with that given in the
tutorial. I am confused.

Apr 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Water Cooler v2 wrote:
http://www.w3schools.com/js/js_whereto.asp

This link is to a JavaScript tutorial on w3schools. The page says that
a script put in the HEAD is executed only when called, whereas one put
in the BODY is executed as the page loads (implying invariably, whether
called or not).


No, it doesn't say that, at least the top two bold statements appear to say
that, but the body of the page seems to say something different. It says
that scripts [containing function declarations] which you intend to be
executed only when you call the functions should be placed in the head
rather than in the body, and that scripts which are to be executed as the
page loads should be put in the body.

They are just recommending a convention, and perhaps haven't explained it
clearly enough. You can have a different convention if you prefer:

I don't necessarily agree with the bit about immediate execution scripts
going in the body: it can be useful to set css styles using Javascript as
the page loads. e.g. a non-scripted display:none to hide all the stuff
which needs scripting, followed by a document.write to output a style rule
to make it visible again. Other ways of manipulating css to work in a non-
script environment tend to end up with unsightly page redraws.

Also I would say that anything defining functions shouldn't be in the head
at all: it should be in an external file linked from a script element in
the head.

Inline script in either head or body should be avoided as far as is
reasonable.

Apr 18 '06 #2

P: n/a
Water Cooler v2 wrote:
<snip>
<SCRIPT langauge="JavaScript" type="text/javascript">
<!--
void function bar()
{

<snip>

I cannot believe this got away uncommented. The unary - void - operator
takes an expression as its right hand side operand so the following
function must become a function expression. In a formally correct ECMA
262 implementation the optional function Identifier ('bar') would not
leak out into the containing scope. The formally correct interpretation
of this code is that it is completely worthless as the - bar - function
would be inaccessible following the expression.

Some well-known implementation bugs mean that the - bar - identifier
will leak into the containing scopes in some browsers, but this code
should be expected to be useless, and likely is useless in at least some
browsers.

In addition, unless something is actually done with the - void function
bar(){ ... } - expression (the resulting - undefined - value is employed
in some way) there is absolutely no point in having the void operator
there at all. VK is virtually the only individual who would consider
doing anything so pointless, and that is just because he attributes
mystical potency to the void operator.

Richard.
Apr 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.