简体   繁体   English

关于如何防止用户单击保存按钮后(通过onbeforeunload)关闭浏览器窗口的建议?

[英]Recommendations on how to prevent users from closing browser window (via onbeforeunload) AFTER the save button has been clicked?

I've found numerous solutions on how to prompt the user about closing a window down before the data is saved but in my scenario, this one page is saving a lot of data and is slow (takes about 30 seconds) and this is AFTER the save button is clicked. 我已经找到了许多解决方案,可以解决如何在保存数据之前提示用户关闭窗口的问题,但是在我的情况下,此页面正在保存大量数据,而且速度很慢(大约需要30秒),这是在单击保存按钮。 What we are finding is that the user is clicking the save button and then navigating or leaving AFTER clicking save. 我们发现用户单击保存按钮,然后导航或单击保存后离开。 Depending on the browser, whatever has been sent to the server isn't necessarily saved (looking at you Chrome). 根据浏览器的不同,发送到服务器的内容不一定会保存(查看您的Chrome)。

I opted to use this solution and almost does what I need except even when I click the save button, I'm still prompted to save the changes since I'm leaving the page. 我选择使用此解决方案,除了我单击保存按钮时,几乎可以完成我需要的所有操作,因为我离开页面后仍会提示我保存更改。 Its like I need to turn off the prompt and then turn it back on after some delay. 就像我需要关闭提示,然后在延迟一段时间后将其重新打开。 Is this even possible? 这有可能吗?

    window.onbeforeunload = function() {
        if ( Flag == 'Y' ) {
            return "Changes made. Don't want to save?";                         
        }
    }

Why don't you try a "save as you go" approach?... You can send small quick requests as the user progress in the whatever data input mechanism you have, and store that on a temporary file on your server, so when the user hits the "save" button or closes the browser window you only send a tiny request, quick enough to allow a safe browser disconnection. 您为什么不尝试“随手保存”的方法呢?...您可以在用户使用任何数据输入机制时随用户进度发送小的快速请求,并将其存储在服务器上的临时文件中,因此当用户点击“保存”按钮或关闭浏览器窗口,您只发送了一个很小的请求,速度足够快,可以安全地断开浏览器的连接。 The server then just reads the temporary file and processes it as if it was send by the user in a single operation. 然后,服务器仅读取临时文件并对其进行处理,就好像它是由用户在单个操作中发送的一样。 May not be easy, but you'll get rid of quite a few headaches. 可能并不容易,但您会摆脱很多麻烦。

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

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