0

I have a few clients that will be using my website, and I want each client to have their own "config" file (EX: location=1 for one computer, location=2 for another). I want to do this using a file I place on the client's machine and then when they access the website the client looks on their own machine and figures out what to load based on what's in that file. This file can be a CSV file, plain text file, or any other kind of file that it needs to be for this to work.

Looking online all I've seen is stuff with file uploader. I don't want them to have to select the file, just have the file contents load and call a javascript function when they do.

Example of file

Location=1
AnswerToQuestion=42

and another file

Location=2
AnswerToQuestion=15

and my JS function

var setAnswerToQuestion = function(answer){
  locationConfig.setAnswer(answer)
}
4
  • How do you present the question and how does the user answer it?
    – jcuenod
    Commented May 23, 2015 at 13:28
  • @jcuenod I don't and they don't. The system would already know the answer based on the answer in the file so the person using the webpage doesn't need to know the question's even being asked. Commented May 23, 2015 at 13:32
  • But where did the file come from? Are you emailing to people or are you using secret government equipment to inject it onto people's computers?
    – jcuenod
    Commented May 23, 2015 at 13:35
  • I am physically going to their location and putting a file on their machine with everything needed for them to run the app at that location. Commented May 23, 2015 at 13:37

2 Answers 2

3

Take a look at localstorage. It's a persistent key/value system that the browser implements to keep data for your website/webapp.

The Basic Principle:

To set a variable:

localStorage.setItem('answer_1', '42');

To get a variable:

localStorage.getItem("answer_1");

I guess if you have lots of answers you would end up with an array/object something like this:

var answers = [42, 15];

Towards a Solution:

You could store and retrieve that by using JSON.stringify

localStorage.setItem('answers', JSON.stringify(answers));

var answers = JSON.stringify(localStorage.getItem('answers'));

Be Educated

Smashing Magazine has a tutorial here
Dive into HTML5 has a tutorial here

14
  • this doesn't work because it initializes everyone to Location:1 AnswerToQuestion: 42. It needs to be localized to that system. Commented May 23, 2015 at 13:19
  • This doesn't initialize anything, this is what you use once you have an answer from them. The examples above are examples...
    – jcuenod
    Commented May 23, 2015 at 13:21
  • how do I get the answer from them? that's what the file was providing Commented May 23, 2015 at 13:24
  • where does the question come from? Surely your UX doesn't involve them saving answers into a file on their computer...
    – jcuenod
    Commented May 23, 2015 at 13:25
  • @MattWestlake WebStorage API is "per origin" which means it's not shared between clients.
    – user188654
    Commented May 23, 2015 at 13:27
1

You can't access files on local machines without using "file upload". You could store your config files on browser localstorage as:

var getConfigData = function() {
    return JSON.parse(localStorage.getItem('config'));
}

var saveConfigData = function(config) {
    localStorage.setItem('config', JSON.stringify(config));
}

var addDataToConfig = function(key, value) {
    var config = getConfigData();
    config[key] = value;
    saveConfigData(config);
}

var config = {
    Location: 1,
    AnswerToQuestion: 42
};

// save new config
saveConfigData(config);

// add new data to config
addDataToConfig('name', 'John Doe');
7
  • this doesn't work because it initializes everyone to Location:1 AnswerToQuestion: 42. It needs to be localized to that system. Commented May 23, 2015 at 13:19
  • Yes, I know @MattWestlake, it's only an example to show you how to store data in user browser. when a usser will login on your page then you could store individual data.
    – Jose Mato
    Commented May 23, 2015 at 13:21
  • @JoseMato Do you know that you can set localStorage using localStorage['key'] = value?
    – jcuenod
    Commented May 23, 2015 at 13:23
  • thanks @jcuenod I missed call saveConfigData. fixed!
    – Jose Mato
    Commented May 23, 2015 at 13:24
  • the issue wasn't the localStorage. the issue is getting the inital information from each client (when each client has their own information). Commented May 23, 2015 at 13:26

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