I need to write a software that uses HTML5 and canvas.
The whole software should be able to run locally, without the need of a server. So I'm only able to use Javascript, no php.
The difficult part: I have to dynamically fetch the content of text-files which are needed during operation.
For example: When the software starts, I need the "config.json". And after the user made some desicions, I need "story1.txt" or "story2.txt" and so on.
My Problem:
I can't use Ajax, because Chrome blocks it - local Files aren't allowed to fetch other files' content. I always get an error-Message.
What I've tried so far:
Load Files with Ajax & jQuery
Chrome doesn't allow me to load the file
Load File into script-Tag
Even if I declare the JSON-File as js-Code, I can't access the content of the loaded File
Load File into invisible Iframe, and read its content
Loading works, and I can see the code. But when I try to access the IFrame's content, I get the Chrome Error-Message again:
"Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match."
Question:
Is there a way to load a Textfile dynamically, or am I forced to use a Webserver?
JavaScript, in most cases, is a client-side scripting language. That means the code runs in the browser so there's nothing you need to do on the server to enable that. The exception would be installing some sort of web server written in Node.
JavaScript cannot typically access local files in new browsers, but the XMLHttpRequest object can be used to read files. So it is actually Ajax (and not Javascript) which is reading the file.
Use Chrome browser and with the Web Server for Chrome extension, set a default folder and put your linked html/js files in there, browse to 127.0. 0.1:8887 (0r whatever the port is set at) in Chrome and open the developers panel & console. You can then interact with your html/js scripts in the console.
You can't modify local files from Javascript.
If you insist on using Chrome, it have some command line flags to allow access to/from local originated files (--allow-file-access-from-files
/ --disable-web-security
). Do note that you need to run entire browser from scratch with those flags - i.e. if there's already any other Chrome windows open flags WON'T have any effect and that effect persists across ALL windows until Chrome is closed, which is, obviously huge hole in security.
You can set up a lightweight local server if you pack your "application" with some kind of automated setup script. This is still not very good, because you'd need to install executable that user might not want or even be completely unable to install due to restrictions.
You can pack your HTML/JS-based app as Chrome extension - extensions have much wider permissions than random code, but then you'd need to either distribute it through Google Play or provide instructions to manually install extensions for your users.
And finally, you can format all the data, including your configuration and text files your mentioned as valid JavaScript code - i.e. pack a story1.txt
to story1.js
like:
var myapp.story1 = "Complete text of story1.txt"
and then just dynamically select stuff you need from corresponding vars or even use DOM manipulation to only load scripts you need through dynamically adding <script>
tags. In my opinion that would be best option because it is less intrusive: it doesn't requires any installation/reconfiguration, it just works out-of-box.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With