Create download <a> link - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Anchor

Description

Create download <a> link

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from  ww  w. j a va2 s. co m
var textToSave = 'this is a test';
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:attachment/text,' + encodeURI(textToSave);
hiddenElement.target = '_blank';
hiddenElement.download = 'myFile.txt';
hiddenElement.click();
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials