views:

238

answers:

5

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?

P.S: I am not allowed to use AJAX

+1  A: 

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.

Luke Schafer
@Luke the corporate IT teams of non-IT core companies are loaded with idiots who would tell you not to use AJAX :-) : And my boss is one of 'em!
Jay
Won't the periodic HTTP request keep the session alive?
matt b
then they are screwed. and so are you if your boss reads that you just called him an idiot...
geowa4
@matt b: not if you keep record of the last user activity.
geowa4
@matt b - George said what I was going to say :)
Luke Schafer
+1  A: 

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.

geowa4
+1  A: 

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.

Jonathan Sampson
Or just tell your boss that you're brilliant and figured out a way to do it with Javascript instead of AJAX
John Meagher
Well, the J in AJAX is javascript. If its not async (A) and not xml (X) based, then he can build a case saying that he's not using AJAX, unfortunately, the example uses JQuery and underneath the hood, it is using the A, J and X of AJAX.
Ryan Fernandes
+1  A: 

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.

OMG Ponies
A: 

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.

Chi