Call a function in hyperlink in JavaScript

Description

The following code shows how to call a function in hyperlink.

Example


<!--  w  w w  . j a  v  a 2  s .  co  m-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function greetVisitor()
{
var myName = prompt("Name?", "");
document.writeln("Welcome " + myName + "!")
}
</script>
</head>
<body>
<P><a href="javascript:greetVisitor()">Click for a greeting</a></p>
</body>
</html>

Click to view the demo

The code above generates the following result.

Call a function in hyperlink in JavaScript