Javascript while loops Tutorials - JavaScript Functions Tutorial

JavaScript Functions?


Functions are used for doing repeated task over and over again, it’s a piece of code on the your page that sits dormant until it is called. Functions do not execute when a page loads, its used when an event occurs. Functions are stored in the head of a document.

Sample Pseudo Code:

function sendMessage() {
   code to be executed
}

The web page below contains a button, when the user clicks the button the sendMessage function is called. An alert message is then thrown from the sendMessage function - try this code out.

Example:


<html>
<head>
<script type="text/javascript">
<!--
function sendMessage() {
    alert("Hello and welcome to LearningSteps.com");
}
//-->
</script>
</head>
<body>
<input type="button" onclick="sendMessage()" 
value="sendMessage">
</body>
</html>

In Javascript it is quiet easy to pass values to functions, you add the value you want to pass into the function call. See the example below


<html>
<head>
<script type="text/javascript">
<!--
function sendMessage(username, password) {
    alert("Hello " + username + “. Your Password is ” + password);
}
//-->
</script>
</head>
<body>
<input type="button" onclick="sendMessage(‘fergal’,'$password')" 
value="sendMessage">
</body>



The output message thrown would now be Hello Fergal Your Password is the password assigned to the php variable $password


The output here is the same as for the FOR loop with 9 being the last number outputted.

javascript tutorials