简体   繁体   English

使用onbeforeunload关闭窗口之前的等待事件

[英]Waiting event before closing window using onbeforeunload

Context : 内容:

I have developped an application which require authentification. 我已经开发了需要身份验证的应用程序。 This application uses events for dialoging with a server. 此应用程序使用事件与服务器对话。 When the server answer, some events are send to the client (UI). 服务器应答后,一些事件将发送到客户端(UI)。

Problem : 问题:

When the user close the page, it is necessary to make a logout on the server. 当用户关闭页面时,有必要在服务器上注销。 With my architecture, it's easy to call a method which perform this logout. 使用我的体系结构,很容易调用执行此注销的方法。 But i would like that the user show the logout progress before closing the webpage. 但是我希望用户在关闭网页之前显示注销进度。 In fact, i would like to close the webpage only when a specific event (for example : disconnection_success), is well received. 实际上,我只想在特定事件(例如:disconnection_success)得到良好接收时才关闭该网页。 Moreover, it's verry important to not launcg another webpage because event is received on the first webpage when the logout is successfull. 此外,不要注销另一个网页非常重要,因为注销成功后,第一个网页上会收到事件。 (Because dialog is done throw XMLHttpRequest) (因为对话框完成后抛出XMLHttpRequest)

Test : 测试:

I already do some test using onbeforeunload but it seems that is difficult to customize the popup. 我已经使用onbeforeunload做过一些测试,但似乎很难自定义弹出窗口。

Do you have some ideas to resolve the problem ? 您是否有解决问题的想法?

BR BR

There are some issues with this, but you're on the right track. 这样做有一些问题,但您的方向正确。 You're right in that you should use onbeforeunload because it is the only event that you can have triggered upon the closing of the browser window. 没错,您应该使用onbeforeunload,因为这是在关闭浏览器窗口时可以触发的唯一事件。 (I know you can use onunload but at that point you have no time to do anything.) The issue here is how much code do you want to execute. (我知道您可以使用onunload,但那时您没有时间做任何事情。)这里的问题是您要执行多少代码。 The onbeforeunload doesn't allow you much time before it starts to unload the page. onbeforeunload在开始卸载页面之前不允许您花费很多时间。

BTW, there are two different scenarios with onbeforeunload: 顺便说一句,onbeforeunload有两种不同的方案:

  • If you return a string inside the onbeforeunload event, it creates the pop-up that you were referring to. 如果在onbeforeunload事件内返回一个字符串,它将创建您所引用的弹出窗口。 The issue here is that with the pop-up, you won't have enough time to execute code 这里的问题是,通过弹出窗口,您将没有足够的时间执行代码
  • The other option is not returning anything. 另一个选项是不返回任何内容。 Instead, call your logout methods. 相反,请调用注销方法。 This should give your code enough time to execute before closing 这应该给您的代码足够的时间在关闭之前执行

I actually had a question very similar to this and ended up solving it myself: How to logout during onbeforeunload/onunload using Javascript 我实际上有一个与此非常相似的问题,最终自己解决了: 如何使用Javascript在onbeforeunload / onunload过程中注销

In your question you state that you want to have a progress bar displayed when they log-out. 在问题中,您声明要在注销时显示进度条。 This is impossible to do when the user closes the browser. 当用户关闭浏览器时,这是不可能的。 At the moment they close their window, you have lost all control, except for in the onbeforeunload (and onunload but don't use this), and that is why your code needs to be executed there. 在它们关闭窗口的那一刻,您失去了所有控制权,除了在onbeforeunload(和onunload,但不要使用此功能)中,这就是为什么需要在此处执行代码的原因。 With that being said, you could anchor your logout button (I'm assuming you have one on your application) and have it display the progress bar. 话虽如此,您可以锚定注销按钮(我假设您的应用程序上有一个按钮)并显示进度条。

Just think about what could happen if you actually did have that kind of control - where you could pop up windows and progress bars when the user is trying to close their browser window. 试想一下,如果您确实拥有这种控件,会发生什么情况-当用户试图关闭其浏览器窗口时,您可以在其中弹出窗口和进度条。 You could pop up anything and restrict the user from having any reliable functionality. 您可以弹出任何内容并限制用户使用任何可靠的功能。 That is why it was programmed that the onbeforeunload (and unload) events are the only ones possible to access the closing of a browser. 这就是为什么编程将onbeforeunload(和unload)事件是唯一可能访问浏览器关闭的事件的原因。 These events have some pretty strict guidelines to them that prevent any kind of possible mis-use. 这些事件对他们有一些非常严格的指导原则,可以防止任何可能的滥用。 I understand the problem you're having, I was there and it stinks, but I think that is your only option if you were going to use onbeforeunload. 我了解您遇到的问题,我在那里并且很臭,但是如果您要使用onbeforeunload,那是您唯一的选择。

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

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