Skip to content

A href JavaScript | Using JavaScript inside the A Link Tag

  • by

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:

A href JavaScript inside anchor tag

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading