onbeforeunload 或单击浏览器后退按钮需要帮助

发布于 2024-10-02 11:48:37 字数 175 浏览 3 评论 0原文

如果用户单击浏览器的后退按钮,那么我希望出现提示并要求确认。如果用户单击“确定”,则应导航至 xx.html。如果用户单击“取消”,则应阻止导航。我该怎么做?

注意:我已经尝试过 onbeforeunload 方法,但它适用于所有导航操作。例如,单击页面上的链接也会触发此事件并向用户显示消息。

If a user clicks the browser's back button, then I want a prompt to appear and ask for confirmation. If the user clicks 'OK', then it should navigate to xx.html. If the user clicks 'Cancel', then it should prevent the navigation. How can I do this?

Note: Already I tried the onbeforeunload method, but it is working for all the navigation actions. For Example, clicking the link on the page will also fire this event and show the message to the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄居人 2024-10-09 11:48:37

你不能。您能做的最好的事情就是使用 onbeforeunload 并在单击普通链接时删除该事件:

<script type="text/javascript">
window.onbeforeunload = function() {return "Are you sure?"}
</script>

<a href="somewhere.html" onclick="window.onbeforeunload = null;">Leave</a>

You can't. The best you can do is use onbeforeunload and remove that event when clicking on a normal link:

<script type="text/javascript">
window.onbeforeunload = function() {return "Are you sure?"}
</script>

<a href="somewhere.html" onclick="window.onbeforeunload = null;">Leave</a>
记忆消瘦 2024-10-09 11:48:37

无法区分 onbeforeunload 事件中的导航操作。您可以通过删除每个链接的 onclick 处理程序中的 window.onbeforeunload 处理程序来禁用页面上的所有链接/表单,或者 表单的 onsubmit。但是,您仍然无法判断用户是否单击了后退、前进、书签等或在地址栏中输入了新地址。

There's no way to differentiate between navigational actions in the onbeforeunload event. You could disable it for all links/forms on the page by removing the window.onbeforeunload handler in each link's onclick handler, or onsubmit for forms. However, you will still be unable to tell whether the user clicked back, forward, a bookmark, etc or typed a new address into the address bar.

赤濁 2024-10-09 11:48:37

首先,您需要捕获“beforeunload”事件(这里之前的答案就是这样做的)。

// Display browser warning message when back/forward/reload or hyperlink navigation occurs
$(window).on('beforeunload', function(e) {
  console.log('page navigation captured');
  return 'By leaving this page you will lose the data you have entered here.';
});

更重要的是,您希望允许从某些元素进行不间断的导航(例如带有“allow-navigation”类的链接)。实现的方法是在单击您选择的链接时删除事件处理程序。

// Disable capture for certain elements
$('.allow-navigation').on('click', function(e) {
    $(window).off('beforeunload');
    console.log('page navigation allowed');
});

适用于您的链接类型:

<a href="#" class="allow-navigation">Click me</a>

这样您就可以控制页面导航。浏览器后退/前进/等仍然打开确认框。但您选择的链接将正常工作,不会出现任何中断。

希望这有帮助。

Firstly, you need to capture the "beforeunload" event (which previous answers here did).

// Display browser warning message when back/forward/reload or hyperlink navigation occurs
$(window).on('beforeunload', function(e) {
  console.log('page navigation captured');
  return 'By leaving this page you will lose the data you have entered here.';
});

More importantly, you want to allow non-disrupted navigation from certain elements (e.g. links with class "allow-navigation"). The way to achieve is to remove the event-handler when your links of choice are clicked.

// Disable capture for certain elements
$('.allow-navigation').on('click', function(e) {
    $(window).off('beforeunload');
    console.log('page navigation allowed');
});

Will work for your links of the type:

<a href="#" class="allow-navigation">Click me</a>

This way you can control the page navigation. Browser back/forward/etc still open the confirmation box. But the links you choose will work without any disruption.

Hope this helps.

飘然心甜 2024-10-09 11:48:37

你可以做到

$(window).bind("卸载前", function() {
    return "您确定要离开此页面吗?"; // 返回你想要的任何消息
}); 

如果 xx.html 是其历史记录中的页面,则无需添加
任何东西只要返回一条消息,它就会自动放置按钮

编辑:在你认为小提琴不能回答问题之前先看看它
询问http://jsfiddle.net/Lxq93/

编辑:没有明确的方法可以仅在背面显示消息
据我所知,我也看过并且看到了唯一的按钮
获取后退按钮的方法有
$(window).bind("beforeunload",function(){return;})
$(window).onbeforeunload = function(){return;} 他们都拿起
当页面刷新或任何其他导航离开时
当前页面。这是目前可以提供的最佳答案
2014年

编辑:

以上是所有卸载事件

http://jsfiddle.net/Lhw2x/85/

一直在通过其所有较低的对象搞乱它,它无法通过 javascript 完成,直到有人决定让所有主要浏览器为每个卸载事件使用特定名称,而不仅仅是一个卸载事件,

所以答案是否定的,你不能强制当他们尝试转到其他页面时,在确认上单击“确定”时导航到特定页面

,并且后退按钮没有特定的事件名称,它与卸载事件中的多个不同事件共享其事件,因此您无法返回通过javascript实现按钮特定功能

you can do this

$(window).bind("beforeunload", function() {
    return "Are you Sure you want to leave this page?"; // Return whatever message you want
});​

also if xx.html is the page in their history than you dont need to add
anything just return a message it will automatically put buttons for
you

Edit: see fiddle before you assume it does not answer the question
asked http://jsfiddle.net/Lxq93/

Edit: there is no definitive way to display a message only on the back
button in my knowledge i have also looked and have seen the only
methods that pick up on the back button are
$(window).bind("beforeunload",function(){return;}) and
$(window).onbeforeunload = function(){return;} and they both pick up
on when the page is refreshed or any other navigation away from the
current page. This is currently the best answer that can be provided
in the year 2014

Edit:

above is for all unload events

http://jsfiddle.net/Lhw2x/85/

been messing with that through all its lower objects it cant be done through javascript till someone decides to make all major browsers use a specific name for each unload event instead of it just being an unload event

so the answer is no you cannot force a navigation to a specific page when they click okay on the confirmation when they try to go to a different page

and back button does not have a specific event name it shares its event with several different events all in the unload event so you cannot make a back button specific function through javascript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文