Go to: Aardvark download page.

JavaScript

"JavaScript functions" might sound very sophisticated, but they are actually very simple. Let's take a look at this example!

This code:

alert("Hello world!")

...will open an alert box with the text "Hello world!" as soon as the code is parsed by the browser.

This code however:

function myFunction(){
alert("Hello world!")

}

...will do absolutely nothing!

So what's the point with functions you may ask yourself?

The point is that you now have full control over the execution of the code. To make the alert box pop up, like in the first example, you "call" the function.

There are a number of ways to call a function, but I will only demonstrate one type in this tutorial.

If you make a text link and put "javascript:myfunction()" (no quotes) in the URL box you get a link that looks something like this (if you check your source code).

<a href="javascript:myFunction()">Click here</a>

When you click the link the alert box will pop up!

Here is the code and instructions if you want to test this in a page:

Paste this in the head section of the page:

<script type="text/javascript">
<!--
function myFunction(){
alert("Hello world!")
}
// -->
</script>

Paste this in the body section of the page:

<a href="javascript:myFunction()">Click here</a>

If you want to code to execute "onload" paste this in the head section (and skip the link):

<script type="text/javascript">
<!--
alert("Hello world!")
// -->
</script>

Test link

Back to tutorial index <<