Introduction
Overview
History
Advice
Books
Tutorials
Source Code
Top 100
Reference

Beginners
Sample Code
IDE
Syntax
Variables
Boolean Logic
Strings
Arrays
Conditional Flow

Intermediate
Date-Time
Math
Functions
Event Handlers
Error Handling

Advanced
Objects
Dynamic HTML
Cookies
Multimedia
CGI
Java Applets
Distribution

Community
Web Sites
Mailing Lists
USENET
Vendors
News

GBIC >> JavaScript >> Sample Code
JavaScript is placed between the <script> </script> tag pair. The tag pair (and code) may be placed anywhere. The code is executed by the browser as it is read (except for functions which can be called later). JavaScript code is usually placed in the <head> section so that

<html>
<head>
<script language = "JavaScript">    
document.write ("Hello World!");
</script>
</head>
<body>
</body>
</html>

JavaScript code can also reside in an external file, which can be called like this:

<script language="javascript" src="myscript.js"*gt;</script>

The .js extension is not a requirement - any file name may be used. If the external file is found, the JavaScript is used and any JavaScript code found within the tag pair is ignored. If the external file cannot be found, then any JavaScript code found within the tag pair is used.

JavaScript can also be used to respond to an event, such as a click. The Event Handler tutorial has more details, but here's a simple example:

<a href="" onClick="popupFunc();">

JavaScript may be used to generate a replacement URL for an anchor tag, such as in this sample code:

<a href="javascript:mfunction()">Click Here</a>

Finally, JavaScript used to provide the value of any attribute of an HTML tag, such as in this sample code:

<body text=&{favorite_color()};>