1

I have this javascript function which requires some variables I would like to be called from an external .txt file instead:

<script language="JavaScript">

StartCountDown("1070","08/21/2014 15:32 +0300")
StartCountDown("1071","08/21/2014 16:00 +0300")
StartCountDown("1072","08/21/2014 16:00 +0300")

... rest of the function
</script>

would be better if

<script language="JavaScript">

**include stuff from variables.txt here**

... rest of the function
</script>

Is there any way to do this? Thanks.

Ivan
  • 1,274
  • 16
  • 22
  • Are you using any kind of server-side technology? If so, this becomes easier. If not, you can use a client-side build process that would turn your text file into whatever JavaScript you needed. We need more details. – Jeremy J Starcher Aug 22 '14 at 03:36
  • I would prefer doing this client-side. The txt file shouldn't be turned into a javascript but rather be included inside the said function. – Ivan Aug 22 '14 at 03:37
  • Show a practical example of your case. What you need to store in the text file and what you're going to with it. – akinuri Aug 22 '14 at 03:39
  • JavaScript, natively, has no kind of `include` functionality. – Jeremy J Starcher Aug 22 '14 at 03:41

1 Answers1

5

Option 1: load txt into variable

you can load a .txt file synchronously via javascript/ajax. have in mind that your javascript code execution will wait at this point until the file is loaded.

var request = new XMLHttpRequest();
request.open('GET', 'text.txt', false);
request.send();
var textfileContent = request.responseText;

you can also do this asynchronously. although in this case your program logic will have to wait for the file to be loaded:

var request = new XMLHttpRequest();
request.open('GET', 'text.txt');
request.onreadystatechange = function() {
    if (request.readyState === 4) {
        var textfileContent = request.responseText;
        // continue your program flow here
    }
}
request.send();

execute javascript from variable (discouraged)

it is also possible to have javascript code evaluated out of a string. although this method is discouraged:

eval(textfileContent);

Option 2: include javascript through script

if it is javascript that you want to load, another solution is to add a script tag to the dom:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "text.txt";
document.body.appendChild(script);

Option 3: use json to initialize variables (best for your needs)

if you are just loading variable values json might make more sense to you. create a textfile like this:

[
    ["1070","08/21/2014 15:32 +0300"],
    ["1071","08/21/2014 16:00 +0300"],
    ["1072","08/21/2014 16:00 +0300"]
]

read about the json syntax here

than load the json file like this:

var request = new XMLHttpRequest();
request.open("GET", "json.txt", false);
request.send(null);
var data = JSON.parse(request.responseText);

your data is now in an array. you might want to access the values in a loop:

for ( var i = 0; i < data.length; i++ ) {
    var entry = data[i];
    var year = entry[0];
    var date = entry[1];
    StartCountDown(year, date);
}

Option 4: merge before runtime

if you have a preprocessor setup you might want to use it to put the files together. alternatively the unix command line tool cat can easily merge two files:

cat file1.txt file2.txt > new.txt
dreamlab
  • 3,321
  • 20
  • 23
  • that's not correct JSON. Trailing commas are invalid. – Seth Aug 22 '14 at 03:57
  • The "include javascript through script" worked like a charm, apparently because those variables also include a function so they need to be called as javascript as well. Thank you very much for your detailed solutions, much appreciate it! – Ivan Aug 22 '14 at 04:02
  • you should think about the json solution. and execute you scripts in a the loop like: `StartCountDown(year, date)` – dreamlab Aug 22 '14 at 04:03