0

I want to load some data to the page using Javascript.

The easiest way for me is to use js Array, like below:

phones = ["14 211 122", "11 212 131", "666 132 123"];

But I feel that I shouldn't keep this kind of data in .js file. I think it should be in a separate, easier to access and edit file like .txt.

On the other hand, I'm not sure if loading data from .txt will be secure?

What should I use? Some kind of jQuery .load()? What do you suggest?

Thanks for help!

3
  • that depend on who will maintain this file, is it a developer, layman who then...security is another step, which shouldn't be consider here as it should be globally available to any action (data dependent). what you should make sure of is speed and reliability. Commented Oct 12, 2013 at 13:26
  • Adding to gwillie's point above, expanding on the speed, if it suits you more to host it in a text file or equivalent and you only have a few files, then don't worry too much. On the other hand, if you accessing many of these files then you should start thinking about performance overhead with all these requests (which would only be made worse on a mobile connection), and just embed it into the javascript (or combine it all into one text file). Commented Oct 12, 2013 at 23:28
  • So keeping this kind of data inside javascript isn't such a bad idea? It is better then keeping it in a separate (even combined in one) .txt file? Is it correct considering purposes of files (html for structure and data; css for look and simple animations; js for action etc.)? If so, I guess it should be defined at the begining of .js in order to find and change it easily later on, shouldn't it? Commented Oct 13, 2013 at 16:39

1 Answer 1

1

You can use something like :

var data;
$.ajax({url: 'values.txt'}).done(function(d) { 
    //assuming values.txt is formatted like : 1,2,3,4,5,6;
    data = JSON.parse('[' + d + ']');
});

However I'd recommend just storing the javascript directly instead of having to reparse it like that.

Sign up to request clarification or add additional context in comments.

2 Comments

Won't work. You are assigning to the argument data, not the variable you declared outside the handler ;)
My bad, was half asleep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.