This is a job for AJAX. Instead of using document.location to redirect to another page, what you need to do is:
- Create an XMLHttpRequest object.
- Use that object to make a request (i.e., invoke your perl cgi script).
- Define a response handler that will receive the output of your script and insert it into your page, you can create an empty div element to act as a placeholder for this.
- After handling each response, setup the next request (this is how you would loop through all your dates)
Here's a simple demo I had laying around that does something similar:
<html>
<head>
<script type="text/javascript">
function createRequestObject() {
var req;
if (window.XMLHttpRequest) { // Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if (window.ActiveXObject) { // Internet Explorer 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
// error creating the request object,
// (maybe an old browser is being used?)
alert('There was a problem creating the XMLHttpRequest object');
req = '';
}
return req;
}
// Make the XMLHttpRequest object
var http = createRequestObject();
sendRequest();
function sendRequest() {
// Open perl script for requests
var now = new Date();
http.open('GET', 'http://' + document.domain + '/cgi-bin/ajax/demo.
+pl?nocache='+now.getTime(), true);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4 && http.status == 200){
var response = http.responseText; // Text returned FROM perl scrip
+t
if(response) { // UPDATE ajaxText content
document.getElementById("ajaxText").innerHTML = response;
setTimeout('sendRequest()', 5000);
}
}
}
</script>
</head>
<body>
<p>The message is...</p>
<div id="ajaxText" >Hello World!</div>
</body> </html>
This is just a demo. The demo.pl cgi script simply prints out a random text string:
#!/usr/bin/perl
use strict;
print "content-type: text/html\n\n";
my @messages = (
"This is message 1",
"This is message 2",
"This is message 3",
"This is message 4",
"This is message 5",
);
# print a random message
print @messages[rand @messages] , "\n";
The response handler calls this script every 5 seconds using setTimeout(). Note that I append the epoch time to the cgi script query string so that the browser won't simply cache the first response and use it over and over (that's a common ajax gotcha). When you view the html page in your browser, you should see the message change randomly every 5 seconds.
I hope that helps get you moving in the right direction. Good luck!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.