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