<meta http-equiv="refresh" content="3"/>
Some browsers (e.g. Safari) honor this directive, and refresh the page as
specified, but Firefox is not among them.
Is there a way to get Firefox to refresh the page as requested by this
directive?
Note, I am aware that AJAX may be a better way to achieve basically the same
effect of fetching new data from the server at regular intervals, but this
script is only a streamlined example of a much larger piece of legacy code
that I must work with, and retooling it for AJAX is not an option at the
moment.
FWIW, the server in this case is Apache/2.2.3 (Ubuntu).
If someone is interested in seeing this script in action, I copy it below.
Thanks in advance!
Kynnjo
Below is the script. It is a very modified version of the script given in
http://www.stonehenge.com/merlyn/LinuxMag/col39.html
When the script works as intended, the user should initially see this:
Counting to 20
... continuing ...
and at regular intervals the page will be reloaded, to show increasing
amounts of content, e.g. something like this:
Counting to 20
1
2
3
4
5
6
*... continuing ...*
and finally the user will see this:
Counting to 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
When the script fails to work properly, the browser appears frozen for about
20 seconds, and then it displays only the last output shown above.
Here's the script. It requires a couple of Perl modules (Cache::FileCache
and Digest::MD5) that are not part of the default Perl installation. When
the script is first visited (assuming a bare URL, without any URL-encoded
parameters), the script creates a new session ID, caches a data stub under
this ID, and forks; the parent simply sends a 302 redirect directive whose
URL is the scripts original URL plus the session ID as a URL-encoded
parameter; the child enters a loop that every iteration (of 20) sleeps for 1
second and then adds some more data (namely in the next number in the
sequence 1..20) to the cached data. When the script is visited with a
session ID in its URL, it fetches and displays the cached data associated
with the session ID. If more data is expected, the <head> section of this
response includes the directive <meta http-equiv="refresh" content="3"/>.
#!/usr/bin/perl -T
use strict;
use warnings FATAL => 'all';
no warnings 'once';
$| = 1;
my $MAX = 20;
my $DELAY = 3;
my $cache = get_cache_handle();
( $ENV{ QUERY_STRING } || '' ) =~ /session=(\S+)/;
unless ( my $session = $1 ) {
# this is a fresh visit (i.e. no session id yet)
$session = new_session_id();
$cache->set( $session, [ 0, '' ] ); # no data yet
if ( my $pid = fork ) { # in parent
my $url = ( $ENV{ SCRIPT_URI } || '' ) . "?session=$session";
print <<EO302
Status: 302 Moved
Location: $url
EO302
}
elsif ( defined $pid ) { # in child
close STDOUT; # so parent can go on
my $buf = '';
for ( 1 .. $MAX ) {
sleep 1;
$buf .= "$_\n";
$cache->set( $session, [ 0, $buf ] );
}
$cache->set( $session, [ 1, $buf ] );
exit 0;
}
else {
die "fork failed: $!";
}
}
else {
# returning to pick up session data
my $data = $cache->get( $session );
die 'internal error' unless $data and ref $data eq 'ARRAY';
my ( $refresh, $continuing ) =
$data->[ 0 ] ? ( '', '' ) :
( qq(<meta http-equiv="refresh" content="$DELAY"/>),
'<p><i>... continuing ...</i></p>' );
my $html = <<EOHTML;
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Counter</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
$refresh
</head>
<body>
<h1>Counting to $MAX</h1>
<pre>
$data->[ 1 ]</pre>
$continuing
</html>
EOHTML
my $length = length( $html );
print <<EOPAGE;
Content-Type: text/html; charset=ISO-8859-1
Content-Length: $length
$html
EOPAGE
}
exit 0;
sub get_cache_handle {
require Cache::FileCache;
Cache::FileCache->new(
{
namespace => 'counter',
username => 'nobody',
default_expires_in => '30 minutes',
auto_purge_interval => '4 hours',
}
);
}
sub new_session_id {
require Digest::MD5;
Digest::MD5::md5_hex( Digest::MD5::md5_hex( time() . {} . rand() . $$ ) );
}
<script>
//enter refresh time in "minutes:seconds" Minutes should range from 0 to
inifinity. Seconds should range from 0 to 59
var limit="0:30"
if (document.images){
var parselimit=limit.split(":")
parselimit=parselimit[0]*60+parselimit[1]*1
}
function beginrefresh(){
if (!document.images)
return
if (parselimit==1)
window.location.reload()
else{
parselimit-=1
curmin=Math.floor(parselimit/60)
cursec=parselimit%60
if (curmin!=0)
curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
else
curtime=cursec+" seconds left until page refresh!"
window.status=curtime
setTimeout("beginrefresh()",1000)
}
}
window.onload=beginrefresh
</script>
______________________
Ryan Dellolio
Yanaboo Enterprises, Consulting, Research
http://www.yanaboo.com/
http://ryan.yanaboo.com/
> _______________________________________________
> web-developers-general mailing list
> web-develop...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/web-developers-general
>
> Howabout a Javascript solution?
>
<snip>
Thanks for your suggestion.
I'm trying to avoid JavaScript for this, if at all possible, but I decided
to give your idea a try, just out of curiosity. I modified your original
script to this:
<script>
var delay = 3;
var calls = 0;
function maybe_refresh() {
if ( !document.images ) return;
if ( calls > 0 ) {
window.location.reload();
}
else {
calls += 1;
setTimeout( 'maybe_refresh()', delay * 1000 );
}
}
window.onload = maybe_refresh;
</script>
...and replaced the <meta http-equiv="refresh".../> directive with it.
To my surprise, I saw no difference, either with Safari (which worked as
desired) or with Firefox (which failed exactly is it did before)!
Very puzzling.
One other puzzling fact that I did not mention before is that Live HTTP
Headers shows only two GET requests for the entire interaction, whereas I
expect around 8. This is true whether the reloading is done via the <meta
http-equiv="refresh".../> or via JavaScript.
Kynn
> Hi! I have a test CGI script (written in Perl) that, under some
> circumstances, responds with a page that includes the following in its
> <head> section:
>
> <meta http-equiv="refresh" content="3"/>
>
> Some browsers (e.g. Safari) honor this directive, and refresh the page as
> specified, but Firefox is not among them.
Very strange. Testing here, Firefox 3.5 does honour this directive.
Here are two things which might be preventing it working for you
1) Perhaps you have auto-refresh disabled in your preferences settings.
Enter this in the URL bar
about:config
Now scroll down to find the line with "Preference Name"
accessibility.blockautorefresh
If the preference's "Value" is not false, then reset the preference.
2) Perhaps you have an add-on extension which blocks auto-refresh.
Try starting Firefox in Firefox's "Safe Mode".
If the problem goes away in Firefox's "Safe Mode", then disable
your extensions one by one until you find the culprit.
> Is there a way to get Firefox to refresh the page as requested by this
> directive?
3) If all else fails, try adding the page's own URL in the meta header; for example:
<meta http-equiv="refresh" content="3; URL='./index.html'" />
--
Regards
Ralph