Wednesday, June 25, 2008

Js functions

JS functions:

JavaScript functions will be executed only when the event happens
or by a call to that function. To keep the browser from executing a
script when the page loads, we can define it as a function.




In this external script 'jsfunc.js ' contains only only a function. This script can be called from the main page.
window.alert("welcome to my page"); is for pop up message .




When the onload event happens ( means if we open that page), from body it will automatically call the fun() function. Actually that event has called the function.

Argument passing and return value:

We can pass arguments through functions as we are doing in C programming language. For declaring a variable 'var' keyword and returning a value 'return' keyword also uses. Rewriting the external script 'jsfunc.js' as below





From page, onload event calls the fun() and this function call add() with arguments 2,3 . The add() function return the added value through 'x' variable to 'sum' variable.The document.write function display the value of the variable in the page.

No comments: