Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?
You are not logged in.
Pages: 1
Hello, Ive been searching for it, and my conclusion is, that XMLHttpRequest is bad. Anyone got ideas?
dmLoadContent();
function dmLoadContent() {
var client = new XMLHttpRequest();
client.open('GET', '/words.txt');
client.onreadystatechange = function() {
console.log(client.responseText);
}
client.send();
}
If you want to have a file selector popup then I think you can do that, but otherwise I'm pretty sure XMLHttpRequest is the only way
Unless you want to use cookies I guess
(File selector thing: https://developer.mozilla.org/en-US/doc … input/file)
Offline
Depends. Are the files on the server or are you trying to access files on the client's computer?
In case of first:
Nah, XMLHttpRequset should be fine in combination with AJAX. As you currently have.
https://stackoverflow.com/a/13329895
In case of second:
For security reasons you can't just go and read files from the client on your own accord (for obvious reasons). If you still need client files, you can use something like this.
https://stackoverflow.com/a/26298948
Offline
Depends. Are the files on the server or are you trying to access files on the client's computer?
In case of first:
Nah, XMLHttpRequset should be fine in combination with AJAX. As you currently have.
https://stackoverflow.com/a/13329895
In case of second:
For security reasons you can't just go and read files from the client on your own accord (for obvious reasons). If you still need client files, you can use something like this.
https://stackoverflow.com/a/26298948
I think atm he is just loading the page from an html file on his computer, in which case I would have thought that loading local files should be allowed, but maybe not I guess
Offline
You should put a [JS] tag in the title.
And then carry on googling. This subforum isn’t a search engine, and it’s a waste of everyone’s time to fetch you an answer to a generic question like that.
One bot to rule them all, one bot to find them. One bot to bring them all... and with this cliché blind them.
Offline
You could use the fetch API. It can load pretty much any type of file, and is recommended over XmlHttpRequests now. Also, if you want to load a local file using fetch, you can't; you need to host it on a server, because you can't fetch from file://
Offline
You could use the fetch API.
Oh wow... That looks useful
Its still experimental at the moment, so we probably shouldn't use it yet, but when its finished then I'm definitely going to be making use of this
Not sure that it would solve the problems with loading local files though, as I'm guessing that it still has the cross origin restrictions are still in place
Offline
you can't fetch from file://
But what can?
1448 wrote:you can't fetch from file://
But what can?
I'm pretty sure only the file selector thing can, if you want to do something else then you'll need to find another way to do it
What are you trying to use this for anyway? We might be able to help you to find alternatives if we have some background info.
Offline
Anatoly wrote:1448 wrote:you can't fetch from file://
But what can?
I'm pretty sure only the file selector thing can, if you want to do something else then you'll need to find another way to do it
What are you trying to use this for anyway? We might be able to help you to find alternatives if we have some background info.
Saving data.
^ true.
This also depends on how much you want the user to be frolicking with it. Are they throwing it places? Either way, localStorage is pretty cool. 5MB max per domain is useful. Depending, again, on what you're storing.
Offline
I spoke to him about this in game earlier, it seems like he wants to use this for loading a manual sort of configuration file, which localstorage isn't really for...
I've suggested that file inputs are probably the best way to do it, unless he wants to set up a website or something (when he can use text files)
Edit: Just thought, he might be able to get around the limitations by referencing it as a script assuming he doesn't want to write to it... (create a js file with a single const whateverData = { ... })
Edit 2: Wait... I'm sure I've managed to load local json files before....
Edit 3: This code seems to work...
var request = new XMLHttpRequest();
request.open("GET", "[filename].json", true);
request.responseType = "json";
request.onload = function() {
//do something with request.response
};
request.send(null);
Why did you not just use this in the first place? Did it not work or something?
Not sure, but I think it should work with any type of file, not just JSON
Offline
Offline
Okay, there are many free websites.
Which one can save .txt/ .json files?
Also, I'm saving javascript arrays in the files.
Okay, there are many free websites.
Which one can save .txt/ .json files?
Also, I'm saving javascript arrays in the files.
I have a feeling that I might have misunderstood you before...
Do you also want to change the file, or is it just something that you load data from and occasionally manually change?
If you just want to load it, you can either use XMLHttpRequest or the javascript file thing I mentioned earlier, theres no need to use an external website.
If you also need to save the file, using an external website sounds like more effort than its worth... (By set up a website I meant run it locally on your computer)
Offline
You can't edit local files unless you have a web server. You should really be using one for this kind of projects.
It's easy to configure Apache to mount a web server in your own computer, and even PHP (you're going to want this one to easily work with files) and MySQL (which you should be using to store your config).
You can even view your page more comfortingly without having to mess around with relative links when reading from file:/// (can't use / at the start of a link because it would lead to the root directory of the system, not the page).
Offline
Discussed this with AnatolyEE a bit more, and it seems like its a browser thing.
Firefox (which I'm using) allows you to load local files if you're testing a website locally, but some other browsers don't seem to allow this for some reason...
Offline
use localStorage instead for saving & loading data
Offline
Pages: 1
[ Started around 1734495837.9261 - Generated in 0.468 seconds, 12 queries executed - Memory usage: 1.68 MiB (Peak: 1.91 MiB) ]