This method is a little risky because it uses the eval method, so there could be unexpected results. But as long as the the contents of this text file are as you say and don't include any quote characters on either side of the = sign, it should work out. Also, due to the way that eval works, the variables produced will be string values. I see that some/most of them appear already to be strings. This means that if you wish to use them in later code as numbers, something must be done to convert them into valid javascript numbers. I set the update frequency to 105000 milliseconds which is half of the update period for the file, so should be accurate enough. Each time the file is requested it will be with a new query string based on the current time, so any updates to the file will be used (rather than a cached version)
Code:
(function(){
function loadXmlHttp(url) {
var f = this;
f.xmlHttp = null;
/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
/*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
try {f.ie = window.ActiveXObject}catch(e){f.ie = false;}
@end @*/
if (window.XMLHttpRequest&&!f.ie||/^http/.test(window.location.href))
f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
else if (/(object)|(function)/.test(typeof createRequest))
f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
else {
f.xmlHttp = null;
// Internet Explorer 5 to 6, includes IE 7+ when local //
/*@cc_on @*/
/*@if(@_jscript_version >= 5)
try{f.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){try{f.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){f.xmlHttp=null;}}
@end @*/
}
if(f.xmlHttp != null){
f.xmlHttp.open("GET",url + '?bust=' + new Date().getTime(),true);
f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
f.xmlHttp.send(null);
}
else alert('Your browser does not support AJAX!'); // substitute your desired request object unsupported code here
}
var re = new RegExp('=([^&]*)&', 'g');
loadXmlHttp.prototype.stateChanged=function () {
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href)))
eval(this.xmlHttp.responseText.replace(re, '="$1";'));
}
function updateValues(){
new loadXmlHttp('txt_2_script.txt');
setTimeout(updateValues, 105000);
};
updateValues();
})();
Change the highlighted txt_2_script.txt to the address of the file.
Bookmarks