Forum Moderators: open
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
/* this function is equivalant to PHP's $_COOKIE function */
function get_cookie ( cookie_name ) {
var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
if ( results ) {
return ( unescape ( results[2] ) );
}
else {
return null;
}
}
/* this function will be executed every 30 seconds */
function reloadDiv() {
if(get_cookie("logged")=="in") {
$("#responseContainer").load('page.php');
}
else {
window.location="login_form.php?error=Your session has expired.";
}
}
setInterval("reloadDiv()", 30000);
</script>
<body onLoad="reloadDiv();">
/* this function will be executed every 30 seconds */
function reloadDiv(the_container_id) {
if ($('#'+the_container_id).length) {
if (get_cookie("logged")=="in") {
$('#'+the_container_id).load('page.php');
}
else { window.location="login_form.php?error=Your session has expired."; }
}
}
reloadDiv() is being sucesfully called onLoad. But it does not continue to execute every 30 seconds.
$("#responseContainer") is null
<script type="text/javascript">
reloadDiv();
window.onload=function() { setInterval("reloadDiv()", 30000); };
</script>
It only calls the function once at the beginning.
...this message keeps appearing every 30 seconds:$("#responseContainer") is null
load('page.php') that is preventing your script from locating #responseContainer in subsequent calls - as others have mentioned. Fotiman: I would use Firebug to inspect the elements on your page after the first load to see if you can see responseContainer. Maybe the HTML within page.php is invalid and causing your DOM structure to break?
function reloadDiv() {
if(get_cookie("logged")=="in") {
$('#responseContainer').load('page.php');
alert("refresh!");
}
else {
alert("Your login session has expired");
window.location="login_form.php?error=Your session has expired.";
}
}
<div id="responseContainer">Loading</div>
<script type="text/javascript">
reloadDiv();
setInterval("reloadDiv()", 30000);
</script>
$("#responseContainer") is null
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Reload Div</title>
<style type="text/css">
#responseContainer { width: 500px; height: 500px; border: 1px solid #000; }
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function(){
setInterval("reloadDiv('responseContainer')", 3000);
});
function reloadDiv(the_container_id) {
if ($('#'+the_container_id).length) {
//if (get_cookie("logged")=="in") {
$('#'+the_container_id).load('test.txt');
//}
//else { window.location="login_form.php?error=Your session has expired."; }
}
}
</script>
</head>
<body>
<h1>Test</h1>
<div id="responseContainer">Loading . . . </div>
</body>
</html>