473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Location and Execution of JavaScript code

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
2 1890
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Margaret MacDonald | last post by:
I'm seeing a problem that has me flummoxed. The only thing I can think of is that I'm violating some rule I don't know about. I have some code that does some processing and then does a...
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
10
by: Roland | last post by:
Hello, the example code is the following(the number in parentheses at the beginning are just for reference)(The complete HTML file is at the end of this article): (1)window.location =...
2
by: JHB | last post by:
Hi, How can I do a location.replace when I use a form, like when I use a href? This works. <a href="Ny HTML-side20.htm"; method="post" id="frm" name="BrugerHovedSide"...
13
by: John Smith | last post by:
I am using IE 6.0 from http://www.javaworld.com/javaworld/jw-07-1996/jw-07-javascript-p2.html I gather that "If you need to test a number of command lines, you can reduce the keystrokes by...
4
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is...
2
by: Jeff | last post by:
I'm very new to javascript and need only a small amount in an asp.net application I'm working on. I apologize if I'm not using the correct terminology to describe the problem. I got the code below...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
2
by: -Lost | last post by:
I have been watching code execution in various situations and I have noticed something. On the first test my example gave me obvious results. One method was far faster than the other. However,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.