嘿伙计们,我正在向用户显示一个javascript对话框,使用window.onbeforeunload来处理用户点击后退按钮的情况。 我的代码正在发挥作用,但如果他们点击“离开此页面”(不同浏览器中的消息不同),我很难重定向用户。
// Set check var false to begin with
// This is set true on submit btns using onclick event
submitFormOkay = false;
// If we are on check-availability page
if( window.location.href.indexOf( 'check-availability' ) != -1 )
{
// If back button is clicked
window.onbeforeunload = function()
{
// Only show dialog if submit btn wasn't clicked
if (!submitFormOkay)
{
// Show dialog
return "Use of the browser back button will lose form data.\nPlease use the 'Previous' button at the bottom of the form.";
// If 'leave' this page was clicked'
// do 'window.location = window.location'
}
}
}
提前致谢。
大卫
答案 0 :(得分:0)
如果他们点击“离开此页面”,则浏览器将完成用户在弹出对话框之前尝试执行的任何操作。
但如果他们确实导航回来,他们仍然会在你的网站上,对吧?因此,当您重新请求该页面时,您可以做任何您需要做的事情(假设它没有被缓存)。
换句话说......在服务器端检测用户是否单击了Back,然后执行他们单击Previous时所做的任何操作。