Link to home
Start Free TrialLog in
Avatar of Mulith
Mulith

asked on

Merging Javascript Popup with Javavscript countdown?

Hi All,

I currently have a javascript popup that appears when the user is approaching the end of their session and asks if they would like to renew the session. At the moment the popup just says that the session will time out in 50 seconds. What I would really like is for the seconds to count down until the session expires.

The Javascript popup is also not very pretty so if there is a more elegant way of doing it that won't get blacked by popup blockers then feel free.

This is the current code:

<?php
  $cookie_life = 60;
  session_set_cookie_params(60);
  session_name('test_session');    
  session_start();    
  
  session_regenerate_id(true);
?>

<script type="text/javascript">

var xmlhttp_session;
function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp_session=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp_session=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp_session.onreadystatechange=cfunc;
xmlhttp_session.open("GET",url,true);
xmlhttp_session.send();
}

function extend_session()
{
loadXMLDoc("session_extend.php",function()
  {
  if (xmlhttp_session.readyState==4 && xmlhttp_session.status==200)
    {
      //document.getElementById("myDiv").innerHTML=xmlhttp_session.responseText;
      lefttime = <?php echo $cookie_life; ?>;
      show_session_alert = true;
    }
  });
}

var lefttime=<?php echo $cookie_life; ?>; 
var interval; 
var show_session_alert = true;
interval = setInterval('check_session()',10000); 


function check_session() 
{ 
   lefttime = lefttime - 10; 
   if (show_session_alert && (lefttime <= 50)) {
     show_session_alert = false;

     if (confirm("Your session will expire in "+lefttime+" seconds! Do You want to extend Your session?")) { 
       extend_session();  
     }     
   }   
}


</script>
page 1

Open in new window


This is a countdown script that I like Javascript Countdown


The only problem with this is that it looks like it's called from a div element which I think is not possible on the javascript popup.

Is there any way to merge these two to get the result I'm looking for or is there a better way to do it that won't get flagged by popup blockers?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Proculopsis
Proculopsis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mulith
Mulith

ASKER

Hi Proculopsis,

Yeah that looks pretty good. If the page scrolls down quite a long way would i be able to get it to popup in the center of the users screen even if they are way down the page?

Would I then need to create a function to hide the div again when the user clicks "Extend Session" or "Cancel" within the div?
Avatar of Mulith

ASKER

Still heard nothing on this ... Could anyone else help?