无法在Javascript中保存或输出? [英] No saving or outputting is possible in Javascript?

查看:89
本文介绍了无法在Javascript中保存或输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

我想知道是否至少有可能在Javascript中将某些值保存在计算机上的单独文件中?在 关于Javascript的w3schools.com教程 中进行了说明" JavaScript没有任何打印或输出功能".这是否意味着使用Javascript绝对不可能在计算机上保存信息?

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?

推荐答案

您可以使用以下代码:

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

然后,您的浏览器应下载一个文件,其中包含contentVar内部的所有内容.

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

其缺点是您无法设置文件名.以跨浏览器的方式执行此操作很困难,但是像往常一样,这里有一个库可以帮助您! FileSaver.js

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

这篇关于无法在Javascript中保存或输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆