javascript - saveTextAsFile is not defined -
i'm trying save file getting error 'savetextasfile not defined' see below
<script type='text/javascript' src='savetextasfile.js'></script> <textarea id="inputtexttosave" style="width:512px;height:256px"></textarea> <table> <tr> <td>filename save as:</td> <td><input id="inputfilenametosaveas"></input></td> <td><button onclick="savetextasfile()">save text file</button></td> </tr> </table>
i have function savetextasfile() in file savetextasfile.js in same directory html:
function savetextasfile() { var texttowrite = document.getelementbyid("inputtexttosave").value; var textfileasblob = new blob([texttowrite], {type:'text/plain'}); var filenametosaveas = document.getelementbyid("inputfilenametosaveas").value; var downloadlink = document.createelement("a"); downloadlink.download = filenametosaveas; downloadlink.innerhtml = "download file"; if (window.webkiturl != null) { // chrome allows link clicked // without adding dom. downloadlink.href = window.webkiturl.createobjecturl(textfileasblob); } else { // firefox requires link added dom // before can clicked. downloadlink.href = window.url.createobjecturl(textfileasblob); downloadlink.onclick = destroyclickedelement; downloadlink.style.display = "none"; document.body.appendchild(downloadlink); } downloadlink.click(); }
add slash beginning of script tag src make absolute host, otherwise, if in url "localhost/foo/bar/"
, browser try load file "localhost/foo/bar/savetextasfile.js"
, if add slash, try load "localhost/savetextasfile.js"
<script type='text/javascript' src='/savetextasfile.js'></script>
Comments
Post a Comment