Skip to content

Call href from JavaScript | Example Code

  • by

Using getElementById you can access and assign the href value. Even you can perform click events from script code.

document.getElementById('fo').href = new URL("https://eyehunts.com/")

Call href from JavaScript Example

HTML example code: Assign the alert box script in href, you can add URL values also.

The href I’d like to activate is a “javascript:FUNCTION” kind of link.

<!DOCTYPE HTML>

<html>
<body>

	<a href="javascript:alert('test');" id="fo">JS Auto Click</a>

   <script>
<!DOCTYPE HTML>

<html>
<body>

	<a href="#" id="fo">JS Auto Click</a>

	<script>
		document.getElementById('fo').href = "javascript:alert('test');"

       document.getElementById('fo').click();//fake a click on the link
   </script>

</body>
</html>



Output:

Call href from JavaScript

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

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *