Link to home
Start Free TrialLog in
Avatar of dnsoft
dnsoft

asked on

Problem with javascript:setInterval

Hi,

I use a code to display the counter result and to refresh that value I actually refresh all the page, I'd like to refresh the script only without refreshing the full page but I don't know how to modify the code to make it work.
Here's the actual code using the page refresh:
<body bgcolor="#566079" align="center">
<font face="Verdana" size="1" color="#C0C0C0">
<script language="JavaScript" src="../cgi-bin/online/online.cgi?output=javascript"> </script> visitors online
</font>
</body>


And here's the code I'd like to see:
<head>
<script language="JavaScript">
function vonline()
TO BE REPLACED BY ?
</script>

</head>
<body onLoad="javascript:setInterval('vonline()',5000);" bgcolor="#566079" align="center">
<font face="Verdana" size="1" color="#C0C0C0">
<script language="JavaScript" src="../cgi-bin/online/online.cgi?output=javascript"> </script> visitors online TO BE REPLACED BY ?
</font>
</body>

Thank you for all help...
G.
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Hello dnsoft,

Try

<body onLoad="setinterval('vonline()',5000);" bgcolor="#566079" align="center">

Regards,

Roonaan
Avatar of dnsoft
dnsoft

ASKER

Hello,
Thanks for your answer but that will not change anything if no script has been specified in the <head> section.
Regards
Can you post the output of

http://YourDomain/cgi-bin/online/online.cgi?output=javascript

Also if you can look in online.cgi, can it generate HTML output? May be something like

http://YourDomain/cgi-bin/online/online.cgi?output=html

if later is possible, post that output too. It would be much simpler if your online.cgi can generate HTML output instead of javascript.
<body onLoad="setinterval('vonline()',5000);" bgcolor="#566079" align="center">
<font face="Verdana" size="1" color="#C0C0C0">
<div id="visitors><script language="JavaScript" src="../cgi-bin/online/online.cgi?output=javascript"> </script></div> visitors online
</font>
</body>

and here is the head
<head>
<script language="JavaScript">
function vonline() {
document.getElementById('visitors').innerHTML='<scr' + 'ipt language="JavaScr' + 'ipt" src="../cgi-bin/online/online.cgi?output=javascr' + 'ipt"> </scr' + 'ipt>';
}
</script>
Avatar of dnsoft

ASKER

To amit_g:
Yes, it can generate html output.
To cem_turk:
I tried your code but it doesn't display the result, it just displays "visitors online", nothing else...

Thanks for your replies and comments.
Best regards
G.
SOLUTION
Avatar of Sinoj Sebastian
Sinoj Sebastian
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
please post the code of the cgi called in the script tag
Avatar of dnsoft

ASKER

to sinoj: I tried but that doesn't work, thanks anyway.

I changed partially the solution provided by cem_turk:
<html>
<head>
<script language="JavaScript">
function vonline()
{
document.getElementById('visitors').innerHTML='<scr' + 'ipt language="JavaScr' + 'ipt" src="../cgi-bin/online/online.cgi?output=javascr' + 'ipt"> </scr' + 'ipt> visitors online';
}
</script>

</head>
<body onload ="setInterval('vonline()',5000);" bgcolor="#566079" align="center">

<font face="Verdana" size="1" color="#C0C0C0">
<div id="visitors">
<script language="JavaScript" src="../cgi-bin/online/online.cgi?output=javascript">
</script> visitors online
</div>
</font>

</body>
</html>

With this code:
At first launch it displays the results correctly (i.e. 4 visitors online)
and at specified interval there remains only "visitors online" without the number.
I think there's a mistake in the line document.getElementById('visitors').innerHTML....
but I don't find where.
> .. and at specified interval there remains only "visitors online" without the number.
sound like a browser caching problem
change your call like:


function vonline()
{
document.getElementById('visitors').innerHTML='<scr' + 'ipt language="JavaScr' + 'ipt" src="../cgi-bin/online/online.cgi?output=javascr' + 'ipt&amp;x='+Math.random(42)+'"> </scr' + 'ipt> visitors online';
}
>>to sinoj: I tried but that doesn't work, thanks anyway.
What was the problem?
any error or?
It was working for me
Avatar of dnsoft

ASKER

to ahoffmann:
I tried and it doesn't work (xx visitors at page load and "empty" visitors at intervals)
to sinoj:
at load: visitors (wo numbers)
at intervals: visitors online visitors

Avatar of dnsoft

ASKER

to ahoffmann:

I tried on several computers with the same result (all using IE7)
function resultHandler() {
            // request is 'ready'
            if (myxmlhttp.readyState == 4) {
                // success
                if (myxmlhttp.status == 200) {
            showTextAt.innerHTML = myxmlhttp.responseText;
            alert(myxmlhttp.responseText);                   <<<<<<<<<<<<<<<<<-------------------------- Add this alert
                } else {

What is the out put from this alert?
Avatar of dnsoft

ASKER

to ahoffmann:
sorry I didn't see your request, here's the cgi code:

#!/usr/bin/perl

#Description
#This script shows the users online via SSI or Javascript. Read the readme.htm for
#complete details on installation and usage.

#The number of minutes the user is assumed to be online after a hit. (default 15)
$minutes = 5;

#The output method, text or javascript
$output = "text";

#The style to use while showing the counter
$style = "color: #C0C0C0;text-decoration: none";

#IMPORTANT FOR WINDOWS SERVERS
#Remove the # before chdir and enter the path to online here.
#All \ in the path should be written as \\.
#chdir "c:\\home\\site\\cgi-bin\\online";

after this everything is encrypted...

anyway, I don't think the problem comes from there otherwise I should not be able to execute it at all.
didn't mean the cgi's source but it's output
But looking closer, I guess that it simply returns a number.
Avatar of dnsoft

ASKER

to sinoj:

I've this alert:
<a href="http://www.perlonline.com" style="color: #C0C0C0;text-decoration: none">2</a>

and I guess the 2 is the number of visitors.
I do not understand anymore...
Avatar of dnsoft

ASKER

to ahoffmann:
Yes, a formated number and a link atached to it
Avatar of dnsoft

ASKER

to sinoj:
I tried your code again (without the alert you asked me to add).

before I just refresh the page between changes. (with the results I told you before)
I tried to close IE and then at start it displays first "visitors", it directly changes into "x visitors" and at the specified interval it becomes "visitors online visitors"
Avatar of dnsoft

ASKER

Hi guys,
I will do a (not too) short pause because it's now more than 12 hours I'm busy with this f..... counter.
So, if you still have ideas and want to continue, you're welcome but I wont answer for a while.
just the time to get out of here 1 or 2 hours...

Thank you for your efforts and your help...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dnsoft

ASKER

to amit_g:

Hi, your code works but doesn't refresh the value.
I mean I must close the page and reopen it in order to display the real value.
Just use of a page refresh or of the set interval doesn't modify the displayed value.
Looks like it memorize that value and keeps it until the end.
Avatar of dnsoft

ASKER

I made a lot of tries using these codes aboves (i.e. var sURL = "../cgi-bin/online/online.cgi?output=html";)
and none works after the first time, even if I do a manual refresh.
I also tried to use another cgi script that returns only the counter value in text format without other info and it produces the same result.

This function seems to work correctly only using this command:
<!--#exec cgi="../cgi-bin/vonline/vonline1.cgi" --> via a .shtml page.
to amit_g: is it possible to adapt your code to use the above execution command?

Thank you
Best regards
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dnsoft

ASKER

to ahoffmann:
Do you mean a local caching (my computer ?) or are you talking about the server side ?
If server side, is there something to do ? to ask to the web hosting server ?
if local, I tried on several computers with the same result, so, I think there will be nothing to do, I use default IE settings like 99% of people...
All the codes that we have been given assume that your URL

../cgi-bin/online/online.cgi?output=html

works. If that doesn't work, all of the posted codes won't work. so first ensure that the above URL works. You can test it by directly using that URL in a browser.

http://WhateverDomain/cgi-bin/online/online.cgi?output=html

again make sure that the URL is correct as I have only guessed that your cgi-bin is in root which is true in most but not all cases. Also it would be better if the code only generated count with as less formatting tags as possible. In fact, could you show us what you get when you fetch this URL directly in the browser. Also does it change in every refresh?

As far as

<!--#exec cgi="../cgi-bin/vonline/vonline1.cgi" -->

is concerned, if this URL works, use this one instead of the previous one. Again the code I posted is well tested. As long as underlined URL works, it would refresh the content.

> Do you mean a local caching (my computer ?)
yes

> if local, ..
you either have to switch off local caching, or circumvent the browser's logic as I already suggested. I also suggested that you sniff the request and response. Still waiting ...
Avatar of dnsoft

ASKER

to ami_g:

As I already said, the URL works, it works as a link in a web page and it works as a direct URL in the browser.
So, ../cgi-bin/online/online.cgi?output=html in page and http://WhateverDomain/cgi-bin/online/online.cgi?output=html work, they also work when I refresh manually and using a script to refresh the page.
And about using <!--#exec cgi="../cgi-bin/vonline/vonline1.cgi" --> , this is what I was doing before starting on this forum and that worked, but I wanted to keep it up to date on the page without having to reload all the page, that was the point...

but when I use it in the code you provided me (I talk about all codes, not only yours), it doesn't work, the best code displayed the right value at IE opening and page load, but not on refreshes or specified intervals, some don't display any value anymore and some never update the value.

To finish, I don't say your codes are bad or wrong or whatever, I just say in this case there must be something that prevents to run the scripts, but I don't know what.
I also found another CGI script to give those codes another chance to work but they didn't, they had the same behaviours than the first CGI script used..
About formatting, at the begining there were some, I rapidly change that to just keep a simple text without links, formatting or anything else, it just now sends a 1 or 25 or 121... and nothing else.

Best regards
Avatar of dnsoft

ASKER

to ahoffmann:
Here's the sniffer results (everything's ok):

GET /dn_online/count.htm HTTP/1.1
Connection: close
Accept-Encoding: gzip
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/ag-plugin, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30) Web-Sniffer/1.0.24
Referer: http://web-sniffer.net/

HTTP Response Header
Name Value Delim
HTTP Status Code: HTTP/1.1 200 OK  
Date: Fri, 24 Aug 2007 18:58:24 GMT  
Server: Apache  
Last-Modified: Fri, 24 Aug 2007 11:17:54 GMT  
ETag: "59c1ba-5a2-46cebe62"  
Accept-Ranges: bytes  
Content-Length: 1442  
Connection: close  
Content-Type: text/html


____________________________

HTTP Status Code: HTTP/1.1 200 OK  
Date: Fri, 24 Aug 2007 19:00:55 GMT  
Server: Apache  
Connection: close  
Transfer-Encoding: chunked  
Content-Type: message/http  

Content (0.61 KiB)
TRACE/dn_online/count.htmHTTP/1.1
Accept:image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,application/xaml+xml,application/vnd.ms-xpsdocument,application/x-ms-xbap,application/x-ms-application,application/ag-plugin,*/*
Accept-Encoding:gzip
Accept-Language:en-us
Connection:close
Referer:http://web-sniffer.net/
User-Agent:Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;.NETCLR1.1.4322;.NETCLR2.0.50727;InfoPath.1;.NETCLR3.0.04506.30)Web-Sniffer/1.0.24
> Here's the sniffer results (everything's ok):
that does not look like a request to your online.cgi
Avatar of dnsoft

ASKER

to ahoffmann:

Sorry, I traced the page that links to it, my mistake, here's the direct request to the CGI (get and trace) results:

GET /cgi-bin/vonline/vonline1.cgi HTTP/1.1
Host: www.dnsoft.be
Connection: close
Accept-Encoding: gzip
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/ag-plugin, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30) Web-Sniffer/1.0.24
Referer: http://web-sniffer.net/

HTTP Response Header
Name Value Delim
HTTP Status Code: HTTP/1.1 200 OK  
Date: Fri, 24 Aug 2007 19:20:34 GMT  
Server: Apache  
Connection: close  
Transfer-Encoding: chunked  
Content-Type: text/html  

Content (0.04 KiB)
<palign="center">1visitoronline</p>


______________________________


TRACE /cgi-bin/vonline/vonline1.cgi HTTP/1.1
Host: www.dnsoft.be
Connection: close
Accept-Encoding: gzip
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/ag-plugin, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30) Web-Sniffer/1.0.24
Referer: http://web-sniffer.net/

Read timeout.HTTP Response Header
Name Value Delim
HTTP Status Code: HTTP/1.1 200 OK  
Avatar of dnsoft

ASKER

to ahoffmann:

You were totally RIGHT: this is a caching problem, after disabled it works.
Is there a way to force a page to not cache it ? :-(
Change your URL to have a random QS value in it to trick the web server. You should also be able to disable caching on the webserver individually on this item (online.cgi).

function BodyOnLoad()
{
      var sURL = "../cgi-bin/online/online.cgi?output=html" + "&" + Math.random();
      var sCode = "RefreshContentViaXMLHttp('" + sURL + "', 'MyDiv')";

      window.setTimeout(sCode, 0);
      window.setInterval(sCode, 5000);
}

Math.random() should alter the URL in every request and prevent web-server to assume that it was the same URL.
hmm, I already mentioned to use Math.random() as additional search parameter, don't know why my suggestion didn't work ...
Avatar of dnsoft

ASKER

Hi ahoffmann,

I did what you suggested about Math.random() and it didn't work.
But for your info, the part of code provided by amit_g didn't work neither.

But I found the reason why and the workaround...
Please share with us what you found. Especially why none of the alternatives were working for you.
Avatar of dnsoft

ASKER

Dear all (and especially ahoffmann, amit_g, sinoj and also cem_turk),

First of all, I'd like to thank you for your help, support, big parts of codes and patience.
The script is fully working fine now.
About the last issue "Math.random()", the problem was the place it's placed, so, the way you (and also other on the Internet) suggest to place it "var sURL = "../cgi-bin/online/online.cgi?output=html" + "&" + Math.random();" don't work, I also tried to pass arguments like current time, random letters and nothing worked.
So, I placed an alert at request position to see those random numbers, they were nice (i.e. 0.256665874...) very nice, but ALWAYS the same random number, letter, time... and so, no effect at all.
Then I start thinking (yes, I can too ;-), and instead of place it at declaration time, I asked myself: why not place it at execution time ?
So, I tried: oXMLHttp.open("GET", sURL + "&" + Math.random(), true); in the RefreshContent... portion, and that works fine.

So, I'd like to share with you the full code in case of such issue reappears, here it is:
<head>

<script Language="JavaScript">
function GetXMLHttpObject()
{
      var oXMLHttp = null;
      try
      {
            oXMLHttp = new ActiveXObject("MSXML2.XMLHTTP");
      }
      catch (E)
      {
            try
            {
                  oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (E)
            {
              oXMLHttp = null;
            }
    }

      if ((oXMLHttp == null) && (typeof(XMLHttpRequest) != 'undefined'))
      {
            oXMLHttp = new XMLHttpRequest();
      }

      return oXMLHttp;
}

function RefreshContentViaXMLHttp(sURL, sDivId)
{
      var oXMLHttp = null;
      var oXMLHttp = GetXMLHttpObject();

      oXMLHttp.open("GET", sURL + "&" + Math.random(), true);
      oXMLHttp.onreadystatechange = function()
            {
                  if (oXMLHttp.readyState == 4)
                  {
                        document.getElementById(sDivId).innerHTML = oXMLHttp.responseText;
                  }
            }

      oXMLHttp.send(null);
}

function BodyOnLoad()
{
      var sURL = "../cgi-bin/vonline/vonline1.cgi?output=html";
      var sCode = "RefreshContentViaXMLHttp('" + sURL + "', 'MyDiv')";

      window.setTimeout(sCode, 0);
      window.setInterval(sCode, 5000);
}
</script>

</head>

<body onload="BodyOnLoad();" bgcolor="#566079" align="center">
<font face="Verdana" size="1" color="#C0C0C0">
<div id="MyDiv"></div>
</font>
</body>

</html>

I hope this will help one day...
One more time, thank you very much.
Best regards

Gino
> .. instead of place it at declaration time, ..
damn, very good eys dnsoft, you should earn some grading for that too :-)
You are right. I suggested to add Math.random in BodyOnLoad() which is invoked only once and that explains why you would see same random number as it is saved in the variable and used in setInterval. It should be done in RefreshContentViaXMLHttp method as you have correctly identified.
Avatar of dnsoft

ASKER

Thanks... :-)

I will now accept the solution.
But I've got a problem now because you were 3 to help me and that contribute in fixing this issue.
I'd like to give the 500 to each one but it's impossible.
So, I will try to do it in a fair way and I hope I will not disappoint one or all of you.
Best regards

Gino