<html>
<head>
<title>Stay connected script</title>
<script language="JavaScript">
/**
* Gets cookie value by name
*
* Taken from book "Designing with JavaScript. Creating Dynamic Web Pages"
* Author: Nick Heinle
* (c) 1997, Songline Studios, Inc.
* (p) Songline Studios, Inc. and O'Reilly & Associates, Inc.
*/
function getCookie($name)
{
var $cname = $name+"=";
var $dc = document.cookie;
if ($dc.length > 0)
{
$begin = $dc.indexOf($cname);
if ($begin != -1)
{
$begin += $cname.length;
$end = $dc.indexOf(";", $begin);
if ($end == -1)
$end = $dc.length;
return unescape($dc.substring($begin, $end));
}
}
return null;
}
// this has to be global, so that function stayConnected would not crash
var $reload_interval;
/**
* Checks if the page should be reloaded every 10 seconds
*/
function stayConnected($reload_now)
{
// get checkbox object:
$checkbox = document.getElementById('stay_connected_check');
// see if the checkbox is checked:
if ($checkbox.checked)
{
// set the page to reload in 10 seconds:
$reload_interval = window.setInterval('window.location.reload()', 10*1000);
// if the function has been called from the checkbox:
if ($reload_now)
{
document.cookie = 'stay_connected=true;path=/'; // set cookie to true
window.location.reload(); // reload page at once
}
}
else
{
// if the function has been called from the checkbox:
if ($reload_now)
{
document.cookie = 'stay_connected=false;path=/'; // set cookie to false
window.clearInterval($reload_interval); // remove reload interval
}
// if the function has been called at the bottom of the page or from itself
// and the cookie is set to true:
else if (getCookie('stay_connected'))
{
$checkbox.checked = true; // make checkbox checked
stayConnected(false); // call itself once again
}
}
}
</script>
</head>
<body>
<?php
// write unique 32 character string so that we see when the page is reloaded:
// (this code outputs different value every time it is reloaded)
echo md5(uniqid(time()));
?><br>
<input id="stay_connected_check" name="stay_connected_check" type="checkbox" onclick="stayConnected(true);"> Stay connected.
<script language="JavaScript">
// call the function after the checkbox is loaded:
stayConnected(false);
</script>
<hr>
<a href="http://www.opensource.org/"><img align="right" alt="" border="0" hspace="10" src="http://www.opensource.org/trademarks/opensource/web/opensource-55x48.gif" vspace="2"></a>
<a href="source.php">View source</a>.
<br>The script has been tested and works nicely on Internet Explorer 5.0, FireFox 0.8 and Opera 6.05.
<br>The script has been tested and it does NOT work with Netscape 4.75<br>
<br>© 2004 Emilis Dambauskas under <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a> (open source).
</body></html>