Monday, April 16, 2007

HTML HREF and onclick javascript functions

If you have an HTML HREF element just to use the onclick handler to launch a javascript function, make sure to add a "return false;" at the end of the onclick handler to prevent unwanted navigation. If the "return false;" is not specified, then after the handler is executed, the default action will be run, which is to navigate to the location specified in the "href=".

For example:

<a href="#" onclick="myfunction(); return false;">click me </a>

Alternatively, you could write the above line without the onclick mentioned. Then just put an event handler in your Javascript code.

No comments: