简体   繁体   English

JS 中的确认弹出窗口

[英]A confirmation popup in JS

I am looking into making a confirmation menu in JavaScript to where it will run a set of code depending if you select yes or no.我正在研究在 JavaScript 中制作一个确认菜单,根据您选择是或否,它将运行一组代码。

Now I want it to happen on the window.onbeforeunload event but only when the individual presses "yes" do I want the rest of the code to work.现在我希望它发生在window.onbeforeunload事件上,但只有当个人按下“是”时,我才希望其余的代码工作。 If they press "no" I want the window.onbeforeunload to be cancelled outright.如果他们按“否”,我希望window.onbeforeunload被彻底取消。 I am wondering if it is at all possible and how.我想知道这是否可能以及如何实现。 Here is what I have so far.这是我到目前为止所拥有的。 The reason why I want this is because when I run the script the popup shows up on return but before someone would get to choose to stay or leave.我想要这个的原因是因为当我运行脚本时,弹出窗口会在返回时出现,但在有人选择留下或离开之前。 The click(); click(); feature starts up erasing the information.功能启动擦除信息。 I want the .click();我想要.click(); to start up after someone presses "yes" on the return and only if they press "yes".在有人在返回时按“是”并且仅当他们按“是”时启动。

var validNavigation = false;

function wireUpEvents() {

var dont_confirm_leave = 0; 
var leave_message = document.getElementById("kioskform:broswerCloseSubmit");
var leave_safari = document.getElementById("kioskform:broswerCloseSafari");
      function goodbye(e) {
       if (!validNavigation) {
function disp_confirm()
{
var leaveMessage=confirm("Are you sure you want to leave")
if (leaveMessage==true)
{          if (dont_confirm_leave!==1) {
        if(!e) e = window.event;
        //for IE
        e.cancelBubble = true;
        e.returnValue = leave_message.click();
        //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
          e.stopPropagation();
          e.preventDefault();
        }
        //return works for Chrome and Safari
         leave_safari.click();
         return '';

        //add the code to delete the kiosk information here.
        // this is what is to be done.
      }
    }

   else 
{
Alert("Returning to the page.")
}
  }
  window.onbeforeunload=goodbye;

  // Attach the event keypress to exclude the F5 refresh
  jQuery('document').bind('keypress', function(e) {
    if (e.keyCode == 116){
      validNavigation = true;
    }
  });

  // Attach the event click for all links in the page
  jQuery("a").bind("click", function() {
    validNavigation = true;
  });

  // Attach the event submit for all forms in the page
 jQuery("form").bind("submit", function() {
    validNavigation = true;
  });

  // Attach the event click for all inputs in the page
 jQuery("input[type=submit]").bind("click", function() {
    validNavigation = true;
  });

}

// Wire up the events as soon as the DOM tree is ready
jQuery(document).ready(function() {
  wireUpEvents();
});

为什么不直接使用window.confirm

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

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