This is not a coding on demand service, nor is it even a coding forum.
Members may assist others if they are in the mood or it is pertinent.
TESTED javascript (which is NOT JScript)
Code: Select all
<html>
<head>
<script>
/*
beteljuice JavaScript for multiple asynchronous file grabs
TESTED !!
If you break it - YOU fix it !!!!
*/
var reports_url = ["myfile1.txt", "myfile2.txt"];
var reports_span = ["report1", "report2"];
var xx = reports_url.length;
var ajax_attempt = 0;
start_up();
function start_up() { // make sure at least first DOM element is recognized before running main code
if(document.getElementById(reports_span[0])) {
doIt();
} else {
ajax_attempt++;
if(ajax_attempt >= 50) {
document.write('Page has failed to download fully'); // 50 attempts for page to format
} else {
setTimeout('start_up()', 200);
}
}
} // END function start-up()
function doIt(){
for(reportNum = 0 ; reportNum < xx ; reportNum++) { // step through arrays
if(document.getElementById(reports_span[reportNum])) betelLoader(reportNum); // existance check then process
} // END step through arrays
setTimeout('doIt()', 60000); // start all over again
} // END function doIt()
// ------------------------------------------------------------------------------------------
// Using this function to check existance and grab / display
// ------------------------------------------------------------------------------------------
function betelLoader(thisnum) {
if (document.getElementById) {
var now = new Date();
var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(reports_url[thisnum]+ "?" +now.getTime());
}
if (x) { // got something back
x.onreadystatechange = function() {
try { if (x.readyState == 4 && x.status == 200) { // file exists and received
var repdat = x.responseText
document.getElementById(reports_span[thisnum]).innerHTML = repdat; // o/p the txt file
} else // END if (x.readyState == 4 && x.status == 200)
if(x.readyState == 4 && x.status == 404) { // file does not exist
document.getElementById(reports_span[thisnum]).innerHTML = "!#**@**!"; // o/p ERROR
}
} // END try
catch(e){}
} // END x.onreadystatechange = function()
x.open("GET", reports_url[thisnum]+ '?' + now.getTime(), true);
x.send(null);
} // END if(x)
} // END betelLoader function
</script>
</head>
<body>
Here are the contents of myfile1.txt<br />
<span id="report1"></span>
<br /><br />
Here are the contents of myfile2.txt<br />
<span id="report2"></span>
<br /><br />
So what's the problem ?
</body>
</html>
EDIT: It
was very easy to make an unforced error with this "must be simple" code - I did miss a bracket, use the wrong kind of bracket, and had a logic sequence error - all now fixed and working.
ace2 test