Skip to content

javascript void(0 download)

  • by

The javascript: void 0 downloads will not stop downloading when clicking on the link. For example my link is below in the <a> tag.

<a href="javascript:void(0);" download>download</a>

So when I click on the link to download a void(0) file. If the variable has no value then create <a> tag with no download option and if the variable has a value created <a> tag with download

javascript void(0 download)

Simple example code stop downloading when <a> tagging has no link and href=javascript:void(0). Write a click event handler and prevent the default events like below,

$('a#id-of-it').on('click', function(event) {
  // Do whatever you want
  event.preventDefault();
})

And the html looks like this,

<a href="#" id="id-of-it">Download</a>

or with javascript

document.getElementById("demo").addEventListener("click", function(e){
    e.preventDefault()
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="javascript:void(0);" id="demo" download>download</a>

Complete code

<!DOCTYPE html>
<html lang="en">
<body>

  <script>
    document.getElementById("demo").addEventListener("click", function(e){
      e.preventDefault()
    });
  </script>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <a href="javascript:void(0);" id="demo" download>download</a>

</body>
</html>

Output:

javascript void 0 download

Do 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

Leave a Reply

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