10

I want to create a simple game in HTML5/JS and I don't want the user to run any webserver or to have to connect to a website. (just an HTML page)

But it looks like FileReader can only be used on files type inputs.

Is it possible to have only two files : index.html and foo.txt side by side and to read foo.txt from index.html with something like :

// No input needed, I know waht I want to read
var my_file = new File("foo.txt");
var reader = new FileReader();
alert( reader.readAstext( my_file, "UTF-8" ) );

Any idea ?

MARTIN Damien
  • 996
  • 2
  • 15
  • 36
  • `FileReader` and `File` are not JavaScript built-ins. There're Java classes of the same name. Are you interacting with an applet or something else that provides a bridge to a JVM? – Mike Samuel Feb 27 '12 at 23:43
  • I don't understand how files relate to your HTML5/JS game. What is it that you hope to accomplish by reading a file, and on what machine is this file located? – Mike Samuel Feb 27 '12 at 23:45
  • 7
    @MikeSamuel `FileReader` and `File` are part of javascript since HTML5 : https://developer.mozilla.org/en/DOM/FileReader – MARTIN Damien Feb 28 '12 at 08:08
  • 1
    @MikeSamuel I want to write a game running in a webbrowser (without server just files). And I want my main script to be able to read files with JSON inside to load maps or characters dynamicaly for exemple (as you'll do with a binary game) – MARTIN Damien Feb 28 '12 at 08:10

3 Answers3

10

I believe that this is your answer: How to open a local disk file with Javascript?

In short, you are looking something like this:

<input type="file" id="files" name="file" />

HTML5 allows you to load files which are stored locally on computer, but you cannot select it. User must select file which he/she wants to be loaded.

Just imagine what would happen when developers (or better spoken, hackers) would have access to everyones local data...

Community
  • 1
  • 1
DRAX
  • 30,559
  • 2
  • 26
  • 28
  • This is what I was afraid of... So I imagine that it i not possible to make a complexe game (with a lot a dynamic ressources) this way... I will have to host the game on a webserver a create requests to contents. – MARTIN Damien Feb 28 '12 at 08:12
  • Yes, server is needed for such thing. Other option could be to make website on local server and then convert it to .exe file using some of listed software (I am not sure is it HTML5 compatible yet): https://www.google.com/search?client=opera&rls=en&q=make+website+stanalone+executable&sourceid=opera&ie=utf-8&oe=utf-8&channel=suggest – DRAX Feb 28 '12 at 17:31
1

This is an old question and I'm sure that plenty of people have run into the same problems since but once JS gets towards being a standalone application ( and this is an annoying thing to have to hack one's way around but I guess increasingly JS apps will be client-server anyways ) then it starts to be necessary to put together some supporting tools anyway.

One way to create data in a maintainable way and then pass it to JavaScript that I am using is to write a simple script that takes a set of content files and parses the content into JSON in a big old data.js file. This can then be included and behave exactly the same as regular Javascript objects. One could also use JSON to store the data in the first place, of course, but that is a lot more verbose than something like simple CSV if you have a lot of data for your application.

glenatron
  • 11,018
  • 13
  • 64
  • 112
0

I have a cheat code for this case.

I named my JSON file as a .js : jsonfile.js , which contains my json data as a string variable :

var jsondata = '{ "foo" : "bar" }';

In my index.html, I include it, before my .js with the code :

<html> 
<head> 
<script type="text/javascript" src="mydata/jsonfile.js"></script>
<script type="text/javascript" src="js/mycode.js"></script>

Then I can get my data as JSON object in mycode.js like this :

var data = JSON.parse(jsondata);
alert(data.foo);
Julien Feniou
  • 964
  • 8
  • 9