Archive for April 3, 2007

Invoking a JavaScript Function

Posted in JavaScript, Web, programming on April 3, 2007 by Joey

This is my first blog. I think it’s appropriate to start with a simple JavaScript concept: Invoking a function. This is accomplished via two simple tasks:

First, create the function.
Second, invoke the function on some user action.

Let’s proceed to an example: Say we want to have a function that pops up a box that says ‘Hello’ when a user clicks a button.

First, let’s create the JavaScript function. In an HTML page, enclose the function in a script tag, with language=”JavaScript”:

function DisplayHello()
{
alert(‘Hello’);
}

Second, create the button and include the code to invoke the function. Invoke the function when the button is clicked. The action that calls this is onclick. In an HTML input tag include the following:

type=”button” value=”Say Hello” onclick=”DisplayHello();”

That’s all there is to it.