Archive for JavaScript

Calling JavaScript href

Monday, April 13th, 2009

JavaScript function and code can be executed from href event of anchor tag. Instead of passing URL of page we can pass JavaScript function in href attribute. The page will not submit to any other page, if you are using JavaScript in href. We will show you how to use JavaScript in href and calling JavaScript in href.

It will remain an interactive input for user and user can do function according to their need. We don't need to make button or submit button to execute JavaScript functions. It is easier to do interact with user.

We have to add javascript: before calling function of JavaScript.

<a href=”javascript:callHref()” >Calling JavaScript Href </a >

Full example

<html>
<head>
<title>Calling JavaScript Href</title>
<script>
function callHref()
{
  alert("Function is called from Href");
}
</script>
</head>

<body>
<a href="javascript:callHref()">Calling JavaScript Href </a>
</body>
</html>

Demo