If you Link JavaScript void it will not navigate to a new web page. Instead, it triggers a JavaScript function that is defined in the page’s JavaScript code.
javascript:void(0)
is a special URL that is often used as the value of the href
attribute of an HTML link. he void
operator is used to prevent the browser from navigating to a new web page when the link is clicked.
Link JavaScript void example
Simple HTML code JavaScript void used in HTML link. when the user clicks on the link, the onclick
attribute triggers a JavaScript function called myFunction()
.
<!DOCTYPE html>
<html>
<body>
<a href="javascript:void(0);" onclick="myFunction()">Click me</a>
<script>
function myFunction() {
console.log("Link JavaScript void")
}
</script>
</body>
</html>
Output:
Comment if you have any doubts or suggestions on this Js void topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version