There are a few ways to call a Javascript function from a link. If the function does not return a value, then you may use:

<a href="javascript:Function();">Click me</a>

However, if the function returns a value, then using the above link will cause the page to display the return value.

Another popular implementation uses “#” as the href attribute value, and puts the function call in the onclick attribute:

<a href="#" onclick="javascript:Function();">Click me</a>

This will not cause the page to display the return value, but it will have the affect of returning to the top of the page.

The solution is to use the void operator. The void operator discards the value of its operand and returns undefined. You may then put the call to a Javascript function in the href attribute and not worry about having the page to display the return value:

<a href="javascript:void Function();">Click me</a>

Here’s a test page to demonstrate.

leave a reply?