Page 1 of 1
need a hand with some JSscript
Posted: Sat 11 Oct 2014 4:46 am
by ace2
I have located a Jsscript to reload a text file on open, but having a little issue with adding more that one source file into it
Code: Select all
var req;
function reloadData()
{
var now = new Date();
url = 'lapseoff.txt?' + now.getTime();
try {
req = new XMLHttpRequest();
} catch (e) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (oc) {
alert("No AJAX Support");
return;
}
}
}
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
}
function processReqChange()
{
// If req shows "complete"
if (req.readyState == 4)
{
dataDiv = document.getElementById('currentData');
// If "OK"
if (req.status == 200)
{
// Set current data text
dataDiv.innerHTML = req.responseText;
// Start new timer (1 min)
timeoutID = setTimeout('reloadData()', 60000);
}
else
{
// Flag error
dataDiv.innerHTML = '<p>There was a problem retrieving data: ' + req.statusText + '</p>';
}
}
}
Now i have 2 text files that need refreshing lapseon and lapseoff txt
How can i added to sources and change the //flag error to display text or another text file
sorry i'm not to good with JS scripts

Re: need a hand with some JSscript
Posted: Sun 12 Oct 2014 6:41 am
by ace2
No one???
I would have thought this would be simple to do.
like on load, write the text files in **here** id ID=
<td><abbr title="Time-lapse turned off at this time">***here***</script></abbr></td>
I know have 2 text files does make it a little harder....
Re: need a hand with some JSscript
Posted: Sun 12 Oct 2014 6:57 am
by uncle_bob
ace2 wrote:No one???
It's the weekend, so folks ain't at work with spare time to read forums

Re: need a hand with some JSscript
Posted: Sun 12 Oct 2014 7:16 am
by ace2
uncle_bob wrote:ace2 wrote:No one???
It's the weekend, so folks ain't at work with spare time to read forums

Am I the only one that doesn't have a life?
Re: need a hand with some JSscript
Posted: Sun 12 Oct 2014 11:36 am
by uncle_bob
ace2 wrote:
Am I the only one that doesn't have a life?
Nah, I'm here too

Re: need a hand with some JSscript
Posted: Sun 12 Oct 2014 12:05 pm
by beteljuice
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
Re: need a hand with some JSscript
Posted: Sun 12 Oct 2014 12:17 pm
by ace2
Thanks betejuice, I was just getting frustrated with attempt after attempts trying to get it to do what I wanted....
I shall try this at my own risk as I understand it could and might KILL me..

Re: need a hand with some JSscript
Posted: Sun 12 Oct 2014 2:36 pm
by ace2
Re: need a hand with some JSscript
Posted: Sun 12 Oct 2014 5:20 pm
by beteljuice
C'est la guerre !
Re: need a hand with some JSscript
Posted: Sun 12 Oct 2014 11:01 pm
by beteljuice
beteljuice code fixed and tested in early solution post ....
Re: need a hand with some JSscript
Posted: Mon 13 Oct 2014 1:30 am
by ace2
beteljuice wrote:beteljuice code fixed and tested in early solution post ....
I hate to do this.....But i'm getting some strange outputs
http://www.users.on.net/~ace2/status.htm
called as beteloader.js
http://www.users.on.net/~ace2/statustest.htm
added onto page its self..
I think after this Betejuice needs to have a donation button!!!!!
Re: need a hand with some JSscript
Posted: Mon 13 Oct 2014 1:44 am
by beteljuice
You have 'hidden' characters in your txt file (possibly UTF-8)
Re: need a hand with some JSscript
Posted: Mon 13 Oct 2014 2:04 am
by ace2
Your right, my powershell script is to blame
Code: Select all
$a = Get-Date
$time =$a.ToShortTimeString()
$time|Out-File C:\Cumulus\scripts\lapseresult.txt
Time to nut this one out!!!!
So where is your donate button????
Re: need a hand with some JSscript
Posted: Mon 13 Oct 2014 3:13 am
by beteljuice
Just noticed an oooops:
Code: Select all
function doIt(){
for(reportNum = 0 ; reportNum < xx ; reportNum++) { // step through arrays
if(document.getElementById(reports_span[0])) betelLoader(reportNum); // existance check then process
} // END step through arrays
setTimeout('doIt()', 60000); // start all over again
} // END function doIt()
Should be:
Code: Select all
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()
Corrected in orginal post
Re: need a hand with some JSscript
Posted: Mon 13 Oct 2014 3:51 am
by ace2
Brilliant Betejuice, your a legend, like a knight in Java-armour!!!!
Also fixed the powershell issue, added -Encoding utf8 switch which sorted that out!!!
Instead of having all that code on the page, i added a line to call the Java Scripts called, wait for......beteljuiceloader.js
So once again...thank you thank you