Freelance Writing Jobs | Today's Articles | Sign In

 
Browse Sections

JavaScript Functions and Some Tricks


What exactly is a function?

A function is a block of code given a name. Whenever you refer to a function by its name, the block of code gets executed. A function is usually defined (written) in between the <head> and </head> tags. The name is almost always followed by an opening and closing parantheis pair, like this: function_name ( ). The name of the function comes after the "function" keyword. The code that a function contains is encoded between { and }. A function is defined using the following syntax:

function function_name ()
  {
  body of the function
  }

Now whenever we want to take actions listed in the body of some function, we can simply call the function by its name, like this: function_name ( ).


An Example

<html>
<head>
<title>Simple JavaScript Example</title>

<script language="JavaScript">
<!-- Hiding from old browsers
function do_work()
   {
   window.status='The best place on Cyberspace';
   alert ('The best place on Cyberspace');
   }
// done hiding !-->

</head>
<body>

<a href="http://www.somewhere.com/mypage"
onMouseOver= "do_work()"
onMouseOut= "window.status= ' ' ">
My Homepage
</a>
</body>
</html>


Summary

Today we learned a very important concept in any programming / scripting language - the function. Functions are indispensible tools, without them you can't go any further. Make sure you do understand the concept behind them. Give me feedback / ask questions if things are not clear.

The copyright of the article JavaScript Functions and Some Tricks in JavaScript is owned by Muhammad Ali Shah. Permission to republish JavaScript Functions and Some Tricks in print or online must be granted by the author in writing.

Go To Page: 1 2

Articles in this Topic    Discussions in this Topic