Bypassing a function name in the onclick attribute of the anchor tag, you can call the JavaScript function. Because sometimes it’s a necessity of triggering a function when the user clicks a link.
JavaScript href link call function
Commonly 2 ways do it HTML example code:-
They are both very similar syntaxes, the only difference is the href
attribute value.
The first is href="#"
, the second is href="javascript:void(0)"
. You might also see this syntax href="javascript:;"
, which is equivalent to the second.
href=”#”
<html>
<body>
<a href="#" onclick="hello()">Click here</a>
<script>
function hello(){
alert("Hello Script");
}
</script>
</body>
</html>
href=”javascript:void(0)”
<html>
<body>
<a href="javascript:void(0)" onclick="hello()">Click here</a>
<script>
function hello(){
alert("Hello Script");
}
</script>
</body>
</html>
Output:
Do comment if you have any doubts and suggestions on this JS href topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version