0
\$\begingroup\$

So, I am making a game-like html file that needs to load a few assets. I have a loading animation which uses the timing of the loading files to create a progress bar. This all goes fine. The files all load, and the app launches. However, none of the files can access each other as they are all in different script tags. And I cannot merge them because the JS is never actually put into the html. So what I need to do is to access the local file system (it uses the file:// prototype) and read the file, then stick the text into a script tag. However, all of the examples I have found on the web throw this error:

Xhtmlrequests can only access (list of protocols not including file)

What I need is a JS way to access the files with no input from the user (other than opening the application). How can this be done?

\$\endgroup\$
  • \$\begingroup\$ Please, do not downvote this but rather edit the question, or, if you cannot, add a comment with the changes you think should be made. \$\endgroup\$ – FreezePhoenix Jan 27 '18 at 22:16
1
\$\begingroup\$

First, to explain the problem you ran into trying to access file://:

The user's file system is not directly accessible from any normal browser environment for security reasons. If you would be able to pull any file from a user's computer into javascript everyone's data would've already been made public :)

Now, first ask yourself the question: Is it absolutely necessary to show a progress bar? How big a file are we talking about here? As long as everything loads within a second or two users are usually fine with waiting.

As long as you indicate that the page is loading somehow the user will generally not mind waiting. Just think about how many games you know of that actually show a progress bar instead of a generic "loading" message.

Either way, you probably want to run your game from a webserver, put the scripts in dedicated .js files (if you didn't already do so) and load everything you need with ajax (XMLHttpRequest) calls.

The biggest source of waiting is normally your media files, scripts are likely to only be a small percentage of your overall package, in size.

For a rough progress estimate you could probably divide the amount of files to load by the ones already loaded.

I would recommend reading through at least the first two steps of https://phaser.io/tutorials/getting-started for the webserver part. Even if it isn't directly related to your problem it should still give you a clear idea of how to go about configuring a webserver and solving your problem.

\$\endgroup\$
0
\$\begingroup\$

I found a way to do this. It's not very honest, but whatever. So, when the page loads, the mechanism to load the files loads them, then unloads them while still updating the progress bar. Then, when it's done, it loads a highly packed file that contains all of the scripts that are needed. This is not a good thing if someone finds out about this as they would be ticked that their computer is loading the resources twice.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.