简体   繁体   中英

Closing popups on session expiry

Here is the thing : my webapp has loads of popups and my boss wants 'em closed on session expiry, coz when session expires and an user presses refresh on a popup, he is being shown the logon page -> user logs on -> user is directed to the dashboard. Now, a dashboard screen in a popup is totally uncool. Here is where google got me:

Have javascript to close popup onload. Generate this onload script into the response if session has expired (checking session expiry from jsp and including onload script conditionally).

Do you think this is a good way to it? What is the best practice for this scenario?

PS: I am not allowed to use AJAX

The best way would be an XMLHTTP request to check login and close them if required - do this periodically.

Astute readers (meaning everyone) will notice that this is an AJAX request, but if you phrase it that way it might get accepted as whoever dictated that you 'aren't allowed to use AJAX' is clearly an idiot.

In a past life, I made a popup manager object that maintained what windows were open. You should probably make one of these if not already done. Then, you can use setTimeout to call a function after so many minutes (or whatever time you want) have gone by. This will check for recent activity (probably via AJAX) and close the popup if you determine that the session has expired. If not, call setTimeout again with your new time, properly adjusted for most recent activity.

^^before the AJAX edit.

Well, since you can't use AJAX, can you put something in the url that will tell you it's a popup? Then you'll know not to show the login screen when the user hits reload.

If your boss is asking you to achieve this, without using AJAX, then you're in trouble. He should understand that the only connection a browser has to the server (without refreshing the page) is javascript (what he understands to be ajax).

The best way to do this is to setup a script on the pages to ask the server if the user is still logged in every 30 seconds or so.

setInterval(function(){
  $.get("loggedin.php", function(result) {
    if (!result.isLoggedIn)
      window.close();
  });
}, 30000);

This script assumes you're using the jQuery framework for rapid development of javascript solutions. This also uses JSON (Javascript Object-notation) to test a return-value from the loggedin.php file.

Bottom line, you need to use AJAX. Tell your boss there is no other way. If he still doesn't get it, ask him to balance his checkbook without using math.

An alternative way to implement modal dialogs in a web application is to:

  1. Model the dialog in a DIV, default styled to display: none;
  2. On desired action, inject/append the Modal dialog DIV into the page source
  3. Reset the CSS display so the modal dialog DIV is visible, overlaid on top of the page by setting the CSS z-index property
  4. Make the modal dialog disappear upon either successful execution or the user cancelling out

Because the modal dialog is part of the page source, the dialog will disappear when the session times out. This approach doesn't spawn supporting windows that can be orphaned as the poster is attempting to address. And it fits the requirement of not using AJAX.

You can code these by hand, but I don't really recommend it because of having to support various browser. I suggest looking at the Yahoo User Interface . You can tailor it to suit your needs (IE: only modal dialogs), and it would support AJAX if requirements change down the road.

Beware of spawning modal dialogs from modal dialogs.

In theory, you could avoid AJAX by using a hidden flash widget...

But more practically, AJAX is the 'right' solution, and I think you will have to talk to your boss, determine where this 'no AJAX' rule came from, and convince him that AJAX is the best way to solve this problem.

Does he think AJAX would be take too much time to implement? If so, you should prove him wrong. Does he think it will be hard to maintain? If so, show how simple the code to do this will be, and how widely used the common AJAX libraries are. If your boss is reasonable, then his goal is to what is best for the product, and you should be able to reason with him.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM