javascript - Download textarea content as batch file onclick -
i'm having trouble downloading textarea content on click of link
. want able store following : java -jar (textarea content).jar
and want download
of name 'info.bat'
. so, tried doing text file issue it's storing entire document instead of textarea content. when tried out in jsfiddle it's giving error message asking me post server , download it. don't want send data server, there way out ? jsfddile
this html code -
<textarea id="textbox" rows="1" cols="30"></textarea> <a href="#" download="info.txt" class="button">download</a>
js -
var anchor = document.queryselector('a#button'); var textbox = document.queryselector('#textbox'); anchor.onclick = function () { anchor.href = (textbox.value); anchor.download = 'info.txt'; };
edit :
<html> <head> <title>top</title> <link rel="stylesheet" type="text/css" href="style.css"> <style type="text/css"> body { background-color: #fff; } textarea { position: absolute; top: 30%; left: 48%; } { position: absolute; top:50%; left: 50%; text-align: center; text-decoration: none; } a:link, a:visited { display: block; font-weight: bold; background-color: #98bf21; color: #fff; border:2px solid #98bf21; width: 120px; height: 20px; border-radius: 25px; text-align: center; padding: 4px; text-decoration: none; } a:hover, a:active { color: #000; background-color: #fff; border:2px solid #98bf21; } </style> </head> <body> <textarea id="textbox" rows="1" cols="30"></textarea> <a href="#" download="info.txt" class="button">download</a> <!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>--> <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> <script type="text/javascript"> var anchor = document.queryselector('a.button'); var textbox = document.queryselector('#textbox'); anchor.onclick = function () { var blob = new blob([textbox.value], {type: "text/plain;charset=utf-8"}); saveas(blob, "info.txt"); //window.navigator.mssaveoropenblob(blob, 'mssavebloboropenblob_testfile.txt'); }; </script> </body> </html>
<textarea id="textbox" rows="1" cols="30"></textarea> <a href="#" download="info.txt" class="button" id="button">download</a> <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> <script type="text/javascript"> var anchor = document.queryselector('a#button'); anchor.onclick = function(e) { var textbox_text = document.queryselector('#textbox').value; var textbox_file = new blob([textbox_text], {"type":"application/bat"}); anchor.href = url.createobjecturl(textbox_file); anchor.download = "newtext.txt"; }; </script>
by adding id attribute button, able download content , not whole html code.
Comments
Post a Comment