0

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?

Community
  • 1
  • 1
brilliant
  • 2,805
  • 11
  • 39
  • 57

1 Answers1

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

parchment
  • 4,063
  • 1
  • 19
  • 30
  • thank you very much for this code, but can you, please, tell me what will be the name of the file and what will be the path? – brilliant Jul 11 '14 at 06:55
  • @brilliant Unfortunately, it fill result in a file download that is named whatever the browser wants. In firefox, it's just a bunch of random letters. I'm editing the answer to allow setting the file name. – parchment Jul 11 '14 at 06:57
  • "Doing this in a cross-browser way is difficult, but as always, there's a library to help! FileSaver.js" - I have never used libraries before, don't know how to do it. I am studying that page now. "In firefox, it's just a bunch of random letters" - It's actually okay with me as long as I know the path, but how can I know the path? – brilliant Jul 11 '14 at 07:03
  • It should be saved to your default download folder. You should get a notification that a download is occurring. – parchment Jul 11 '14 at 07:05