简体   繁体   English

在会话到期时关闭弹出窗口

[英]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. 事情就是这样:我的webapp有大量的弹出窗口,我的老板希望他们在会话到期时关闭,因为当会话到期并且用户在弹出窗口上按下刷新时,他会显示登录页面 - >用户登录 - >用户指向仪表板。 Now, a dashboard screen in a popup is totally uncool. 现在,弹出窗口中的仪表板屏幕完全不酷。 Here is where google got me: 这是谷歌给我的地方:

Have javascript to close popup onload. 有javascript关闭popup onload。 Generate this onload script into the response if session has expired (checking session expiry from jsp and including onload script conditionally). 如果会话已过期,则将此onload脚本生成到响应中(从jsp检查会话到期并有条件地包括onload脚本)。

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 PS:我不允许使用AJAX

The best way would be an XMLHTTP request to check login and close them if required - do this periodically. 最好的方法是检查登录并在需要时关闭它们的XMLHTTP请求 - 定期执行此操作。

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. 精明的读者(意思是每个人)都会注意到这是一个AJAX请求,但是如果你这样说它可能会被接受为任何人说你不允许使用AJAX'显然是个白痴。

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. 然后,您可以使用setTimeout在经过这么多分钟(或者您想要的任何时间)之后调用函数。 This will check for recent activity (probably via AJAX) and close the popup if you determine that the session has expired. 这将检查最近的活动(可能通过AJAX)并在确定会话已过期时关闭弹出窗口。 If not, call setTimeout again with your new time, properly adjusted for most recent activity. 如果没有,请使用新时间再次调用setTimeout ,并针对最近的活动进行适当调整。

^^before the AJAX edit. 在AJAX编辑之前^^。

Well, since you can't use AJAX, can you put something in the url that will tell you it's a popup? 好吧,因为你不能使用AJAX,你能在网址中放一些能告诉你弹出窗口的内容吗? 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. 如果你的老板要求你实现这一点,而不使用AJAX,那么你就麻烦了。 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). 他应该明白浏览器与服务器的唯一连接(不刷新页面)是javascript(他理解为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. 执行此操作的最佳方法是在页面上设置脚本,以询问服务器用户是否仍然每30秒左右登录一次。

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. 该脚本假设您正在使用jQuery框架来快速开发JavaScript解决方案。 This also uses JSON (Javascript Object-notation) to test a return-value from the loggedin.php file. 这也使用JSON(Javascript Object-notation)来测试loggedin.php文件的返回值。

Bottom line, you need to use AJAX. 最重要的是,您需要使用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: 在Web应用程序中实现模式对话框的另一种方法是:

  1. Model the dialog in a DIV, default styled to display: none; 在DIV中建模对话框,默认样式为display:none;
  2. On desired action, inject/append the Modal dialog DIV into the page source 在所需的操作上,将模态对话框DIV注入/附加到页面源中
  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 重置CSS显示以使模态对话框DIV可见,通过设置CSS z-index属性覆盖在页面顶部
  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. 它符合不使用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. 您可以根据自己的需要定制它(IE:仅模态对话框),如果需求发生变化,它将支持AJAX。

Beware of spawning modal dialogs from modal dialogs. 注意从模态对话框中产生模态对话框。

In theory, you could avoid AJAX by using a hidden flash widget... 理论上,您可以通过使用隐藏的Flash小部件来避免AJAX ...

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. 但更实际的是,AJAX是“正确”的解决方案,我认为你必须与老板交谈,确定这个“没有AJAX”规则的来源,并说服他说AJAX是解决这个问题的最佳方法。

Does he think AJAX would be take too much time to implement? 他认为AJAX需要花费太多时间来实现吗? 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. 如果是这样,请说明执行此操作的代码有多简单,以及常见的AJAX库的使用范围。 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. 如果你的老板是合理的,那么他的目标是最好的产品,你应该能够与他合作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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