JavaScript Where To
JavaScripts can be put in the body and in the head sections of
an HTML page.
Where to Put the JavaScript
JavaScripts in a page will be executed immediately while the page loads into the
browser. This is not always what we want. Sometimes we want to execute a script
when a page loads, or at a later event, such as when a user clicks a button.
When this is the case we put the script inside a function, you will learn about
functions in a later chapter.
Scripts in <head>
Scripts to be executed when they are called, or when an event is triggered,
are placed in functions.
Put your functions in the head section, this way they are all in one place,
and they do not interfere with page content.
Example
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
</body>
</html> |
Try it yourself »
|
Scripts in <body>
If you don't want your script to be placed inside a function, or if your
script should write page content, it should be placed in the body section.
Example
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>
</html> |
Try it yourself »
|
Scripts in <head> and <body>
You can place an unlimited number of scripts in your document, so you can have scripts in both
the body and the head section.
Example
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>
<body onload="message()">
<script type="text/javascript">
document.write("This message is written by JavaScript");
</script>
</body>
</html> |
Try it yourself »
|
Using an External JavaScript
If you want to run the same JavaScript on several pages, without having to
write the same script on every page, you can write a JavaScript in an external file.
Save the external JavaScript file with a .js file extension.
Note: The external script cannot contain the <script></script> tags!
To use the external script, point to the .js file in the "src" attribute of the <script> tag:
Example
<html>
<head>
<script type="text/javascript" src="xxx.js"></script>
</head>
<body>
</body>
</html> |
Try it yourself »
|
Note: Remember to place the script exactly where you normally would write the script!

Whether you're new to XML or already an advanced user, the
user-friendly views and powerful entry helpers, wizards, and debuggers
in XMLSpy are designed to meet your XML and Web development needs from
start to finish.
New features in Version 2011!
- XML editor
- Graphical XML Schema / DTD editors
- XSLT 1.0/2.0 editor, debugger, profiler
- XQuery editor, debugger, profiler
- XBRL validator, taxonomy editor, taxonomy wizard
- New! Chart creation for XML data
- Support for Office Open XML (OOXML)
- Graphical WSDL 1.1/2.0 editor & SOAP debugger
- JSON editing & conversion
- Java, C#, C++ code generation
- 32-bit and 64-bit versions
- And much more!
Download a free trial today!
|
|
|
 |
W3Schools' Online Certification
The perfect solution for professionals who need to balance work, family, and career building.
More than 6000 certificates already issued!
Get Your Certificate »
|
The HTML Certificate documents your knowledge of HTML.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
|