A href have JavaScript is a way of running JavaScript instead of following a link. And making a link does absolutely nothing when clicked (unless JS events are bound to it).
javascript:
tells the browser going to write JavaScript code
<a href="Javascript: doStuff();">link</a>
Or
<a href="javascript:alert('Hello');"></a>
Or onclick JS
<a href="" onclick="alert('Hello'); return false;"></a>
A href Attribute with JavaScript example
HTML example code:
Use javascript:
inside of href
attribute and got the result that I can write multiple lines in it!
<html>
<body>
<a href="Javascript: doStuff();">link</a>
<script>
function doStuff() {
a = 4;
console.log(a++);
a += 2;
console.log(a++);
if(a < 6){
console.log('a is lower than 6');
}
else
console.log('a is greater than 6');
}
</script>
</body>
</html>
Output:
Do comment if you have any doubts and suggestions on this JS href code.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version