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>


<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!

	<a href="Javascript: doStuff();">link</a>

		function doStuff() {

			a = 4;

			a += 2; 

			if(a < 6){ 
				console.log('a is lower than 6');
				console.log('a is greater than 6');



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