Location and Execution of JavaScript code

W

Water Cooler v2

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

Richard Cornford

Water Cooler v2 wrote:
<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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top