|
Attaching a
JavaScript function to a button is easy; generally done with the onClick event.
For PopUp Maker, say you
made code for the function called doPopUp
(the default name), and have already inserted the
<script>...</script> as per instructions in the
<head>...</head> of your page.
To use a function with any
event, the correct general form is event="someFunction()" -- so
the correct general form is onClick="doPopUp()"
to attach the doPopUp
function to the onClick event.
Since a button is
technically a <form> element, note that, for sake of compatibility
with older browsers, you must wrap the button in form tags. (This is
not strictly required in many newer browsers versions; but it's a good
precaution to do it, anyway, for purposes of best compatibility.)
Putting it together, it
looks like this simple example:
<form>
<input type="button" value="Click Me"
onClick="doPopUp()">
</form>
(Note that it is common to use onClick="javascript:doPopUp()"
-- but not technically required. Per se, the
javascript: prefix is a protocol, and
usually required only within the href= of an <a> anchor tag -- but
not in an event call.)
Sample:
Although the example above
is for our PopUp Maker application, the same general approach is used when
attaching any JavaScript function to a button.
General cautions:
Observe punctuation carefully. Don't forget to include the paired
parentheses at the end of the function name. In cases of functions
other than those make by PopUp Maker, parentheticals may or may not carry
variables; but PopUp Maker parentheticals are always empty.
|