4

I want to create a very simple Javascript game using HTML5 (Canvas). But is it possible to save a simple .txt file and load a simple .txt file. I just need to store like the some simple integers. But I just want to know if javascript is allowed to save and load an external file?

Canvas

Canvas
  • 5,779
  • 9
  • 55
  • 98
  • 1
    You definitely can't write to the local disk, but most browsers do have some sort of [local storage](http://dev.w3.org/html5/webstorage/). – Mike Christensen Jul 30 '13 at 20:07
  • if security doesn't matter at all, you can try `document.cookie`, which is really easy to use – user428517 Jul 30 '13 at 20:12
  • Check out this link; http://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file – JNL Jul 30 '13 at 20:13
  • @MikeChristensen You can force a download from RAM nowadays (see question linked by JNL). – bfavaretto Jul 30 '13 at 20:16
  • HTML5 introduces file system support, and Chrome already implements it: http://www.html5rocks.com/en/tutorials/file/filesystem/ – MatteoSp Jul 30 '13 at 20:18

4 Answers4

4

Since html5 you can use the LocalStorage API. Nowadays almost all browsers support it:

// Check if it is supported in your browser
function supports_html5_storage()
{
      try
      {
        return 'localStorage' in window && window['localStorage'] !== null;
      }
      catch (e)
      {
        return false;
      }
}

//make use of it:
if( supports_html5_storage() == true )
{
   localStorage.setItem("myItem", "myData");
   var myDataString = localStorage.getItem("myItem");
   alert(myDataString);
}
Alex
  • 1,602
  • 20
  • 33
  • does the local storage api still exist? is the another, newer incarnation of this? –  Nov 16 '16 at 10:22
3

On Chrome, you can rely on the FileSystem API (for an intro take a look here). Probably other browsers will soon add support to it.

But, if your need is just "to store like the some simple integers" I would consider local storage.

MatteoSp
  • 2,940
  • 5
  • 28
  • 36
2

In short, no. According David Flanagan's "JavaScript: The Definitive Guide":

Input and output (as well as more sophisticated features, such as networking, storage, and graphics) are the responsibility of the 'host environment' within which JavaScript is embedded.

The bigger question is why. Think about how dangerous it would be if JavaScript could write files to your hard drive. What if any website you visited could access your local file system?

jds
  • 7,910
  • 11
  • 63
  • 101
  • Websites already write to your file-system ( cookies, etc. ). So why javascript should not do so ? If the access is restricted in a good way, I dont see a security-issue. And ofc there is already an API for this. Its called "Local Storage" – Alex Jun 25 '14 at 14:31
  • 3
    @Alex, local storage is an option and was mentioned by everyone since the first comment. But I wanted to explain why JavaScript cannot write to local disk, and I quoted a pretty respected source on the matter. I knew it wasn't going to be the accepted answer, but I thought it shed some light on the issue. – jds Jun 25 '14 at 20:11
1

You can't access the local file system directly with javascript, but it is possible when you let the user interact (for example by letting the user select a file to upload). See http://www.html5rocks.com/en/tutorials/file/dndfiles/

Another possibility is local storage. See http://davidwalsh.name/html5-storage, http://www.w3.org/TR/webstorage/

Damiaan Dufaux
  • 4,427
  • 1
  • 22
  • 33