如何确定用户何时离开网页(不包括某些链接)
情况: 我有一个包含两个表的 Grails 网页。一个表显示人员信息(包括某些标志),第二个表包含带有“添加按钮”的标志列表,允许用户向自己添加给定标志。
现在,有一个保存按钮,单击该按钮会将用户标志的当前“状态”推送到我们的数据库。因此,当用户尝试导航到网站的其他部分时,如果显示未保存的信息,我希望能够显示提示。通过使用每个标志存储的现有 isDirty 布尔值,这很容易。我可以循环遍历人的活动标志并检查它是否脏。如果此人至少包含 1 个脏标志,则在他们尝试离开时我需要显示提示,因为除非他们明确按下按钮,否则该数据不会被保存。
问题:有很多方法可以离开此页面。我正在使用 ,其中 checkForDirtyFlags() 是一个基本的 js 函数,用于检查任何脏标志。但事情是这样的 - 当用户添加或删除标志时,会导致页面重新加载,因为页面的设置方式是重定向到如下所示的 url:
"http://my.url/addFlag/123456"
然后控制器知道将 id 为 123456 的标志添加到当前人员。然而,这不会改变该人在网站中的位置,因为仍然呈现相同的页面(它只包含更新的表格)。所以基本上,当我看到带有 addFlag 或 removeFlag 的 URL 时,我不想提示用户是否确定要离开该页面,因为在用户看来,他们不会离开该页面。
问题:有什么方法可以确定onbeforeunload
期间的目标是什么?这样我就可以在我的 javascript 中添加类似的内容:
function checkForDirtyFlag() {
if( justAdding ) { //We are just adding a flag. No prompt necessary
//Don't do anything
}
else if( justRemoving ) { //We are just removing a flag. No prompt necessary
//Don't do anything
}
else { // In this case, we want to prompt them to save before leaving
alert('You have unsaved data on the page. Leaving now will lose that data. Are you sure you want to leave?');
}
}
如果有任何不清楚的地方,请告诉我,我会尽力澄清。
谢谢!
The situation: I have a Grails webpage with two tables. One table displays a persons information (including certain flags), and the second table has a list of flags with an "add button" that allows the user to add a given flag to themselves.
Now, there is a save button that, when clicked, pushes the current "state" of the users flags to our database. So I want to be able to display a prompt if there is unsaved information being displayed when a user tries to navigate to another part of the site. This is easy enough by using an existing isDirty boolean that each flag stores. I can just loop through the persons active flags and check if it is dirty or not. If the person contains at least 1 dirty flag, I need to display a prompt if they try to leave, because that data won't be saved unless they explicitly hit the button.
The problem: There are many ways to navigate away from this page. I am using<body onbeforeunload="checkForDirtyFlags();">
, where checkForDirtyFlags() is a basic js function to check for any dirty flags. But here's the thing - when a user adds or removes a flag, that causes a page reload because the way the page is setup is to redirect to a url like this:
"http://my.url/addFlag/123456"
The controller then knows to add the flag with id 123456 to the current person. This does NOT change where the person is in the website however, because the same page is still rendered (it just contains updated tables). So basically, when I see a URL with addFlag or removeFlag, I do not want to prompt the user if they are sure they want to navigate away from the page, because in the eyes of the user they are not leaving the page.
The question: Is there any way to determine what the target is during an onbeforeunload
? So that I can have something like this in my javascript:
function checkForDirtyFlag() {
if( justAdding ) { //We are just adding a flag. No prompt necessary
//Don't do anything
}
else if( justRemoving ) { //We are just removing a flag. No prompt necessary
//Don't do anything
}
else { // In this case, we want to prompt them to save before leaving
alert('You have unsaved data on the page. Leaving now will lose that data. Are you sure you want to leave?');
}
}
If any of this isn't clear, please let me know and I'll try and clear it up.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您无法在卸载事件中获取目标位置。我要做的是将保存/提交按钮绑定到一个函数,该函数在按下按钮时禁用卸载事件,从而禁用提示。如果用户尝试通过按返回等方式离开,则会触发卸载事件。
I don't think you can get the target location in unload event. What I'd do is bind the save/submit button to a function that disables the unload event if the button is pressed, therefore disabling the prompt. If the user tries to leave by pressing back etc, the unload event would fire.
为什么不立即将更改推送到数据库,而不必按下“保存”按钮,或者将它们存储在临时数据库中,以便在导航到站点的其他部分时不会丢失未保存的更改。
Why don't you push the changes immediately to the database, without them having to press the Save Button, or store them in a temporary database so that they do not lose their unsaved changes when the navigate to a different part of the site.
我不太确定我是否理解正确 - 但实际上您已经在那里写下了解决方案。为什么不在必要时从
onbeforeunload
中返回
字符串消息呢?例如:
如果您从该事件返回一个
字符串值
,浏览器将处理显示的模式对话框窗口。否则什么也不会发生。I'm not quite sure if I get you right - but you actually wrote the solution already down there. Why don't you just
return
a string-message from within anonbeforeunload
when necessary ?For instance:
If you return a
string value
from that event, the browser will take care of a modal dialog window which shows up. Otherwise nothing happens.