No saving or outputting is possible in Javascript?

邮差的信 提交于 2019-12-11 17:17:03

问题


In this question I just learned how to illicit one desired element out of a Google translate page. There I have also learned that placing anything into clipboard in Javascript is literately impossible (you can do it by prompting a window asking a user to save some value into the clipboard - but you can't do that automatically), which is a rather sad thing to me as I really need to save that element out of a Google translate page automatically.

I wonder if it's at least possible in Javascript to save some values in a separate file on my computer? In the w3schools.com tutorial on Javascript it is stated that "JavaScript does not have any print or output functions". Does that mean that saving info on my computer is absolutely impossible in Javascript?


回答1:


You can use this code:

// Assuming the text you want to save is in variable 'contentVar'
uriContent = "data:application/octet-stream," + encodeURIComponent(contentVar);
window.location.href = uriContent;

Your browser should then download a file that contains whatever is inside contentVar.

The downside of this is that you can't set the file name. Doing this in a cross-browser way is difficult, but as always, there's a library to help! FileSaver.js



来源:https://stackoverflow.com/questions/24690384/no-saving-or-outputting-is-possible-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!