1

我试图让 window.confirm 使用我正在使用的脚本,但它似乎不想工作。发生的情况是,如果该人按下取消它仍然运行代码,可悲的是现在我编写代码的方式弹出框甚至不会出现在 chrome 中。不知道为什么虽然我可以删除 window.confirm 方法并且它仍然会显示一个弹出框,但它只会运行脚本,无论您做出哪个选择。这是代码,谢谢您的帮助。

var validNavigation = false;

function wireUpEvents() {

var dont_confirm_leave = 0; to be able to leave withou confirmation
var leave_message = 'Leaving the page will terminate your Self-Service or Kiosk session.';

function goodbye(e) {
if (!validNavigation) 

    function goodbye() = {
    var x = window.confirm("Leaving the page will terminate your Self-Service or Kiosk session.")
    if (x)
    {
    window.onunload=leave;
    }    
    else            {
    return "" ;
    }
    }}

function leave() {
    if (!validNavigation) {
    killSession();
    }
    }

    //set event handlers for the onbeforeunload and onunloan events
    window.onbeforeunload=goodbye;
    //window.onunload=leave;

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

2 回答 2

1

您的代码中有语法错误。这是您重新格式化的代码,您可以看到您没有正确关闭函数:

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();
        });

更新:更新了代码,不知道是否还有语法错误,但是。我还缺少其他任何错误吗?

于 2012-09-24T14:14:32.990 回答
1

您无法阻止某人离开该页面 - 如果他们想离开,他们可以。阻止此事件以通常的方式传播是行不通的。要显示弹出消息(通常由浏览器提供的文本包围)的方法是从 onbeforeunload 触发的函数中返回字符串。试试这个:

function goodbye() {
  if (!validNavigation)
    return "Are you sure you want to leave";
  else
    return "";
}
window.onbeforeunload = goodbye;
于 2012-09-24T15:02:16.080 回答