Embedding and including Let's first see a simple example:
<html>
<head>
<title>This is a JavaScript example</title>
<script language="JavaScript">
<!--
document.write("Hello World!");
//-->
</script>
</head>
<body> Hi, man! </body>
</html>
Usually, JavaScript code start with the tag <script language="JavaScript">
and end with the tag </script>. The code placed between <head>
and </head>. Sometimes, people embed the code in the <body>
tags:
<html>
<head></head>
<body>
<script>
.....// The code embedded in the <body> tags.
</script>
</body>
</html>
Why we place JavaScript code inside comment fields <!-- and
//-->? It's for ensuring that the Scripts is not displayed by
old browsers that do not suport JavaScript. This is optional, but
considered good practice.The LANGUAGE attribute also is optional,
but recommended. You may specify a particular version of JavaScript:
<script language="JavaScript1.2">
You can use another attribute SRC to include an external file containing
JavaScript code:
<script language="JavaScript" src="hello.js"></script>
For example, the below is the code of the external file "hello.js":
document.write("Hello World!");
The external file is simply a text file containing JavaScript code,
and with the file name extension ".js". Note:
1. Including an external file only functions reliably across platforms
in the version 4 browsers.
2. The code can't include tags <script language...> and </script>,
or you will get an error message.
|