Technology
 

Howto:Include JavaScript in HTML

From DHTML Wiki

Note: You should have at least basic knowledge of HTML and minimum knowledge of JavaScript before reading this.

Contents

[edit] Inserting JavaScript into your Document

[edit] Inside your HTML File

Use the tag <script type="text/javascript"></script>. Insert your JavaScript inside the tag.

[edit] External File

Add the attribute src inside of your <script> tag and use your javascript's filename for it. So for a JavaScript file named "example.js", it should look like this: <script type="text/javascript" src="example.js"></script>.

[edit] Inserting JavaScript in the Inside of a Tag

Note: This only covers basics.

[edit] Specific Script

Inside your tag, add an attribute for either moving your mouse over it (onmouseover) or for clicking on it (onclick). So say you want to change an image tag for the file image.png so that it will display an alert that says, "Hello!" when you click on it. Change the image tag tothis: <img src="image.png" onclick="alert('Hello!')" />

[edit] Functions

Just change the onclick or onmouseover attribute to your function. So if you want to make it so that moving your mouse over the image hello.gif will activate the function greet(), make this code: <img src="hello.gif" onclick="greet()" />