CancelImage Upload

How to call upon a JavaScript function via an a href link tag

There are two ways to call upon a JavaScript function via a HTML link tag. The first is to simply set the onClick property available in most HTML element:

<a href="http://www.yourdestination.com" onclick="javascript:alert('Hello World');">Your text here</a>

The second method is to directly call upon the function within the href property:

<a href="javascript:alert('Hello World');">Your text here</a>

When to use which method

The first method is best used only when the page is about to change its current location. If not directly defining it inside the href may offer a greater amount of control for the programmer.

Of course the location of the page could also later on be changed via the JavaScript function being called upon:

window.location="http://www.yourdestination.com";


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com