save JavaScript variable as file by making an downloadable link - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Anchor

Description

save JavaScript variable as file by making an downloadable 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 w  w  w  .  j  av a2  s .  c o  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