AlfieWebDevTips Homefor new and experienced developers
[Home Home] [Web Development Web Development ] [Code Generators Code Generators ] [Resources Resources ] [Contact Us Contact ] |WebDevTips Privacy Privacy |
Print This Page

You are here : Home - Web Development - Javascript - Getting Started - Variables and Functions

Variables and Functions



So far, you have learnt :

  • how to add javascript to your pages
  • events
  • actions
  • variables
  • functions

Each of the above on their own can be useful but using all of them together can make javascript quite a powerful tool.

If you look back to our intro to functions you will notice that we created 2 separate functions that are identical apart from the text that was used in the alert.

By using variables we can reuse one function and send any text we want to be used in the alert. Take a look at example 4 Let's look at the code for the function

<script language="JavaScript" type="text/javascript"> 
<!-- 
function do_alert(what_text){ 
alert (what_text); 

// -->
 
</script> 
<noscript>Javascript Alert Functions</noscript>

Notice that this time we have put some text in the () - do_alert(what_text). what_text is our variable in this example. Whatever value we send to the function will be assigned to the variable what_text.

Notice that what_text in the alert (what_text) ; line is not quoted. If it was alert ("what_text"); then the alert would have the text what_text written to it. By not using quotes whatever value is assigned to the variable what_text will be displayed.

Now let's look at the code to call the function.

<a href="#" onclick="do_alert('Hello I am example 1');return false;">Example 1</a><br> 
<a href="#" onclick="do_alert('Hello I am example 2 Using the same function as Example 1');return false;">Example 2</a><br>

The event for this example is onclick and the action is to call the function do_alert This time we are passing a value to the function, the value that is sent to the function is between the (). As we are passing a string (see variables) we have to put the value between quotes.

Please note that in our example the value we are sending to the function is contained within single quotes. You have to be careful with quotes. If you look closely at the above example the action for the onclick event is contained with double quotes onclick="" if we had used double quotes for our value things wouldn't work.

The line would have been interpreted as onclick="do_alert(" - the onclick event would not be calculated any further thus breaking the script. So be aware of quotes.

It is recommended that you play with the examples to get a good understanding of how functions and variables work. This is by no means the limit of what javascript can do but by learning the basic principles of calling functions, using variables and events etc you are well on your way to learning lots more.

And don't forget, if you get stuck at anytime then just ask in our forums. Why not let us know what you thought of this guide as well - feedback is always welcome.




16 Nov 2003




Google
 

[back to top of page]   

[labelled with icra -  - this link will open in a new window] [RSS Content]

[Copyright © WebDevTips]