Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Merging Javascript Popup with Javavscript countdown?

Posted on 2012-04-06
3
Medium Priority
?
268 Views
Last Modified: 2012-04-23
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
0
Comment
Question by:Mulith
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 2000 total points
ID: 37815573
You could try a simple hidden div, as in this jsfiddle.
0
 
LVL 1

Author Comment

by:Mulith
ID: 37815622
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?
0
 
LVL 1

Author Comment

by:Mulith
ID: 37846732
Still heard nothing on this ... Could anyone else help?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question