JavaScript Functions:

A FUNCTION is a set (group, collection, etc) of commands (code) that we can re-use by calling it name therefore every function must have a name. Every funtion name must have an opening and closing paranthesis ( ).  Everything else is written between { and } brackets; everything between and including those brackets belong to that function. JavaScript ignores "white space". (show example of differences).

Rules for creating functions:

  • first character must be a letter
  • other characters can be letters, numbers, dashes and underscores
  • NO other characters including spaces
  • CaSE sENtivE
  • don't name a fuction & a variable the same name
  • Capitalize the first letter of each new word except the first. (openAndCenterWindow)
    NOTE: Putting a function on your page (preferably within the head tags) won't do anything unless and until you "call it" to execute.

Common JavaScript Functions:

alert ( )
a small square pop up window with your messge appears and an OK button.
prompt( )
a rectangle pop up window with your message and a single text field appears with an OK & Cancel Button
getTime( )
returns the # of milliseconds between the current date and Jan. 1, 1970



Passing Values IN to a Function:

You send a value IN to a function by placing it inside the parenthesis after the function name. You can place VARIABLES inside the paranthesis, like this:


       function doSomething(strValue)
            { alert(strValue);} 
    


Then in your form code you give the value of the variable called strValue like this:

  
    <FORM> <Input Type=buttonValue="Press
            Me" OnClick="doSomething('I clicked the button')">
   </FORM>
         

Notice the single quote. You do this as to not cause a conflict.

 

Passing Values OUT of a Function:

You send a value OUT of the function by using the keyword "return".


   
       function doSomething()
             return "I have clicked the button now."             }
            <FORM>
  <Input Type=buttonValue="Press Me" OnClick="alert(doSomething())">
</FORM>

You can't use KEYWORDS used in JavaScript. JavaScript IS CaseSensitive !!!!!!!!   **Use the MSDN Library**
Call a function from within another function:

 
       function doSomething( ) {ChangeIt( )

JavaScript

Examples