简体   繁体   English

JavaScript onUnload的替代方法

[英]Alternative for JavaScript onUnload

there is an alternative to the Javascript onUnload ? 还有Javascript onUnload的替代方法吗? I use JSP Jakarta Struts framework with a Servlets . 我将JSP Jakarta Struts框架与Servlet一起使用。

I must know, when is window closing, because not everyone clicks on Logout button. 我必须知道,什么时候关闭窗口,因为不是每个人都单击注销按钮。
How do you handle it in your applications? 您如何在应用程序中处理它?

Generally speaking, it's impossible to determine conclusively whether a visitor is still "using" (or even looking at) your page. 一般来说,不可能确定性地确定访问者是否仍在“使用”(或什至查看)您的页面。 Once the browser has downloaded the page, it's no longer in contact with the server at all, so the page could stay open for a few seconds or a few years and the server would have no idea. 浏览器下载页面后,它根本不再与服务器联系,因此页面可能会保持打开状态几秒钟或几年,服务器也就不知道了。

The unload event is certainly a helpful clue, since it usually fires when the page is unloaded. unload事件当然是一个有用的线索,因为通常在页面卸载时触发该事件。 It's not at all reliable, however. 但是,它一点也不可靠。 There are plenty of situations where it won't fire, and plenty more where even though it does fire your server will remain unaware of it. 在很多情况下,它不会启动,在很多情况下,即使它确实启动了,您的服务器也不会意识到。

For example: the browser (or even the operating system) could crash, or the power could go out. 例如:浏览器(甚至是操作系统)可能会崩溃,或者电源可能会关闭。 A visitor using Wi-Fi could get disconnected, or could carry her iPhone into a tunnel while using your site. 使用Wi-Fi的访问者可能会断开连接,或者可能会在使用您的网站时将其iPhone拖入隧道。

The standard workaround for this — which we all readily admit is terrible — is to let the visitor's session timeout. 我们所有人都容易接受的标准解决方法是让访问者的会话超时。 Even on high-stakes banking websites, you'll find that if you walk away from your computer for four minutes and come back you still have access, but if you walk away for five you're locked out. 即使在高风险的银行网站上,您也会发现,如果您离开计算机四分钟然后再回来,您仍然可以访问,但是如果您离开五分钟,您将被锁定。

This setup certainly finds a lot of false positives: cases where a visitor was still using your page without interacting with it, and therefore is inconvenienced by the timeout. 此设置肯定会发现很多误报:访问者仍在使用您的页面而不与之交互的情况,因此超时会给您带来不便。 It also allows false negatives: cases where the real visitor has walked away, and a malicious user takes over within the timeout period. 它还允许假阴性:真正的访客已经走开,并且恶意用户在超时时间内接管了这种情况。

The only major addition to this scheme we've seen lately is exploitation of JavaScript to visibly log the user out after the timeout period. 我们最近看到的对该方案的唯一主要补充是利用JavaScript在超时时间后明显注销了用户。 Again, we see this on banking websites: leave the page open for too long and when you come back all you'll see is a login screen. 再次,我们在银行网站上看到了这一点:让页面打开时间过长,当您回来时,只会看到一个登录屏幕。 (Of course, this too depends on JavaScript and so is still fallible.) (当然,这也取决于JavaScript,因此仍然容易出错。)

Unfortunately, since HTTP is a stateless protocol, we'll never be able to know for sure what's going on in the browser (especially if JavaScript is off), so cheap workarounds are really all we can use. 不幸的是,由于HTTP是无状态协议,因此我们永远无法确定浏览器中正在发生什么(特别是如果JavaScript已关闭),因此便宜的解决方法实际上是我们可以使用的全部。

You should be aware that Opera does not execute onUnload as you would suspect. 您应该意识到Opera不会像您怀疑的那样执行onUnload。 And you should not rely on javascript to see if users close windows. 而且您不应该依靠javascript来查看用户是否关闭了窗口。

I rely on the servlet session timeout to handle the logout automatically. 我依靠Servlet会话超时来自动处理注销。 You can register a handler for when the session times out to cover any credential cleanup you need to do. 您可以在会话超时时注册处理程序,以涵盖您需要执行的任何凭据清除。 A good example is given here: 这里给出一个很好的例子:

http://www.xyzws.com/Servletfaq/when-do-i-use-httpsessionlistener/7 http://www.xyzws.com/Servletfaq/when-do-i-use-httpsessionlistener/7

There is no guarantee from the client side that the user will always confirm that they have finished using your site. 客户端无法保证用户将始终确认他们已完成对您网站的使用。

I use the onbeforeunload . 我使用onbeforeunload See the documentation 请参阅说明文件

After testing it seems to work on Firefox , Chrome , IE11 and Edge . 经过测试,它似乎可以在FirefoxChromeIE11Edge上运行

I didn't test any other browser.( compatibility list is here ) 我没有测试其他浏览器。( 兼容性列表在这里

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

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