简体   繁体   English

Javascript - 确认框以在用户单击退出按钮时重定向用户

[英]Javascript - Confirm box to redirect user when they click on exit button

My objective is to when the user clicks on the exit button, it should appear a box with 2 buttons, to confirm or cancel the leave.我的目标是当用户点击退出按钮时,它应该出现一个带有 2 个按钮的框,以确认或取消离开。 After the confirm button is clicked, a redirect is needed.单击确认按钮后,需要重定向。

I tried a lot of solutions, but I could only find a solution that I can control what happens after the confirm button.我尝试了很多解决方案,但我只能找到一个我可以控制确认按钮后发生的情况的解决方案。

But I need to do some action after they click on the confirm但我需要在他们点击确认后做一些动作

My code:我的代码:

window.onbeforeunload = function () {
   return "message"
}

That opens a box to confirm the exit, but I can't control the actions after it这会打开一个框来确认退出,但我无法控制它之后的动作

something like this像这样的东西

  const result = confirm('Do this?');
    if(result){
      //redirect here
    }

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

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