build-website-header
spacer-image
 

Starting JavaScript Part 3:

Triggered Scripts and Functions

As we mentioned earlier in part two, scripts are run either automatically as the web page is loaded, or they can be triggered.

A triggered script is executed when something happens -usually when a button is clicked, or when the mouse passes over some part of the page.

The following illustrates a script triggered when a link is clicked. In this case clicking the link causes an alert which displays the date. When you click ok on the alert you are taken to the link in this case a page called date.htm.

 

<html>

<head>
<META NAME="ROBOTS" CONTENTS=" noindex, nofollow">
<title>javascript back button</title>
</head>

<body>
<FONT face="Arial, Helvetica" size="2">
The main page text goes here<br>
 <a onclick="alert('todays date is'+Date())" href="date.htm">get time </a>
</FONT>
</body>

</html>

 

Click here and the script above will run in the frame below. Click here to reset

 

 

JavaScript Functions

A function is code which is used more than once within a JavaScript program. Functions are called from the main program which is contained within the HTML and are normally placed between the header tags at the top of the HTML document.

Regardless of where you place them they must be placed before the code that tries to use them!

The code within the function is only executed when called and not when the page loads. Here in this example the function (green highlight) adds two numbers together and is called from within the main document (yellow highlight).

<html>

<head>
<title>javascript function</title>
<SCRIPT language="Javascript" type="text/javascript">
<!--
function add(a,b)
{
var x=a+b;
return(x);
}
//-->
</SCRIPT>

</head>

<body>
The main text goes here <BR>

<SCRIPT language="Javascript" type="text/javascript">
<!--

document.write("result = "+
add(2,2) +"<BR>")
//-->
</SCRIPT>

</body>

</html>


Why not sign up for the free Newsletter and receive the latest articles and website making  tips and tricks!

External Resources:






Subscribe To the Blog!


 

 

 

 

 

 

 

 

 

 

 


Add to My Yahoo!



Free Newsletter!