简体   繁体   English

如何在离开页面之前警告用户,但不在重定向时

[英]How to warn user before leaving page but not on redirect

I have some pages on my site that allow users to create and edit posts. 我的网站上有一些页面,允许用户创建和编辑帖子。 When a user is on these pages, I'd like to warn them before leaving the page. 当用户在这些页面上时,我想在离开页面之前警告他们。 I can do that like this: 我可以这样:

//Only warn if user is on a New or Edit page
if(window.location.href.indexOf("/new") !== -1 || window.location.href.indexOf("/edit") !== -1  {
            window.addEventListener('beforeunload', function (e) {
                e.preventDefault();
                e.returnValue = '';
            });

//Doing this again because I don't know which version is compataible with all browsers
            window.onbeforeunload = function (e) {
                e.preventDefault();
                e.returnValue = ''
            };
        };

On a New or Edit page, the information in a form gets submitted to the server using JQuery ajax . 在“ New或“ Edit页面上,使用JQuery ajaxform的信息提交到服务器。 The server returns a URL which the user gets redirected to to see the results of their post/update like this window.location.href = result; 服务器返回一个URL,用户可以重定向到该URL以查看其发布/更新的结果,例如window.location.href = result; with result being the URL sent back from the server. result是从服务器发回的URL。

When that code runs to do the redirect, the user is getting the warning that they are about to leave the page they are on. 当该代码运行以进行重定向时,用户将收到警告,即将离开他们所在的页面。 I don't want it to do this on any redirects/navigation that the user has not performed. 我不希望它在用户未执行的任何重定向/导航上执行此操作。 How could I stop/remove the warning in this instances? 在这种情况下,如何停止/删除警告?

UPDATE: This is not a duplicate. 更新:这不是重复的。 This question asks about preventing a beforeunload event happening on a redirect where the user has not requested to move away from the page himself. 此问题询问有关如何防止在用户未请求自己离开页面的重定向上发生beforeunload事件。

Because you may want the event bound in some circumstances but not others within the same window , you'll have to not only add the event handler to the window , but you'll have to remove it as well (under the right circumstance) because even though you are changing the URL of the document loaded in the window , you are not changing the window itself: 因为您可能希望事件在某些情况下受约束,而在同一window内不受其他约束,因此您不仅必须将事件处理程序添加到window ,而且还必须(在适当情况下)将其删除。即使您正在更改window加载的document的URL,也不会更改window本身:

function handleBeforeUnload (e) {
  e.preventDefault();
  e.returnValue = '';
}

//Only warn if user is on a New or Edit page
if(location.href.indexOf("/new") !== -1 || location.href.indexOf("/edit") !== -1  {
   window.addEventListener('beforeunload', handleBeforeUnload);
} else {
   // Remove the previously registered event handler (if any)
   window.removeEventListener('beforeunload', handleBeforeUnload);
}

If you want to force a navigation with window.location.href , you should disable the beforeunload event listener before you navigate. 如果要使用window.location.href强制进行导航,则应在导航之前禁用beforeunload事件侦听器。

Something like this, for example: 这样的事情,例如:

function unloadHandler(e) {
  e.preventDefault();
  e.returnValue = '';
}
window.addEventListener('beforeunload', unloadHandler);

function forceNavigation(url) {
    // Remove the "are you sure you want to leave?" message, then navigate
    window.removeEventListener('beforeunload', unloadHandler);
    window.location.href = url;
}

Call forceNavigation('https://example.com') to navigate without warning the user. 致电forceNavigation('https://example.com')进行导航,而不会警告用户。

JS fiddle here: https://jsfiddle.net/o918wsam/1/ JS在这里拨弄: https : //jsfiddle.net/o918wsam/1/

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

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