Preparation in HTML File |
Learn the procedure of how to use the handling written in JavaScript in HTML file
Three Steps to Use JavaScript File
To use the JavaScript language handling created separately from HTML, you must take the following three steps.
- [Preparation in HTML]: Declare to use JavaScript language.
- [Definition of handling]: Group multiple basic handlings into one, and assign a name.
- [Application of handling]: Write the name of the handling defined (or predefined) to the position in HTML which you wish to execute.
Now let’s see these three steps by actual examples.
Preparation in HTML File -- Declaration by <head>Element
As is the case where CSS was used, you need to declare to use JavaScript language beforehand by a header element of the HTML file. The following is an example.
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript" src="./myscript.js"></script>
The first line declares the program used in HTML file is in JavaScript language.
The second line declares that a file named “.myscipt.js”, which is the group of definitions of handlings described in JavaScript language, is to be used in this HTML file. This means that
all definitions of handlings for JavaScript are described in the file “./myscript.js”.
Hence, the header element of your HTML file using CSS and JavaScript should look as follows.
............
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" type="text/css" href="../css/mystyle.css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript" src="./myscript.js"></script>
<title>○○○</title>
........
</head>
............