[thelist] Refreshing a part of a page

Keith Davis cache at dowebs.com
Tue Jun 12 21:14:40 CDT 2001


Subhendu Mohapatra wrote:
> 
> Hello,
>            How can I refresh a portion of a page. I am displaying a cricket
> score in my site where all text, images everything remains same and only the
> present score = ###   where the ### should refresh and get the value from
> the server ( say from a database ) in every one second. I don't want to
> refresh the whole page to display the current value of score. While reading
> the text ( content ) of the page this value only should dynamically keep on
> changing.

This can be done without using frames or applets using a partial page
refresh. Microsoft used to do this on their site's front page last year.
Most of that page remained static text which they wanted coming from the
cache but some parts changed frequently which they wanted to always come
from the server. iSyndicate also uses this to keep sending fresh
headlines to news sites that use them for content.

Just use an external javascript file with a document.write in the space
you want changed. If the .js file has changed and nothing else has
changed, then a reload will pull the new doc.write off the server and
pull the rest from the cache. 

"the current score of <script src=score.js></score> is too close to
call"
would produce 
"the current score of 6 to 7 is too close to call"
if the code on score.js is
document.write("6 to 7")
just keep rewriting the score.js on the server as the score changes.

You need to create a setTimeout to reload the page. HOWEVER, if you use
an external .js file only a location=location will force the kind of
refresh that will check for lastModifiedDate from the server. IE will
refresh with little visible activity but NN4 will show a definite reload
blink so you might want to test for NN and lengthen the timeout for NN.

There is a variation for this that does not do the lastModifiedDate
check but guarantees that the doc.write will always be refreshed without
checking. Instead of constantly rewriting a static .js file use a
dynamic .cgi to output the doc.write.

<script src=score.cgi?x></script>

Just have the cgi gather the output string from however you are storing
it on the server and print it back to the browser ala:
print qq~document.write("$theScore")~;

What's the query string "?x" all about? The query string is unimportant
on the server side, but the query string tells the browser "this is
always unique data" and will force the browser to always get the new
copy from the server regardless of lastModifiedDate (the filr score.cgi
will not have been modified, just it's output). 

keith




More information about the thelist mailing list