在我的网站我在新窗口中进行考试&我希望如果用户关闭此窗口,他将被重定向到其他页面,如果他按下'ok'确认。但如果按下“取消”,他应该呆在那里。我正在使用的JavaScript是以下。即使点击确认框中的'取消'按钮,窗口关闭
/**
* This javascript file checks for the brower/browser tab action.
* It is based on the file menstioned by Daniel Melo.
* Refer: http://stackoverflow.com/questions/1921941/close-kill-the-session-when-the-browser-or-tab-is-closed
*/
var validNavigation = false;
function endSession() {
$choice = confirm("You will exit your CSA test. Are you sure you want to close the window?");
if ($choice)
window.open('Result.aspx', '_blank', 'toolbar=0, scrollbars=1');
}
function wireUpEvents() {
window.onbeforeunload = function() {
if (!validNavigation) {
endSession();
}
}
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function (e) {
if (e.keyCode == 116) {
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});
}
$(document).ready(function() {
wireUpEvents();
});
这里就点击“确定”按钮的“Result.aspx”窗口打开成功,但这里的问题是,如果用户点击确认框,然后也窗口被关闭,得到“取消”。 请告诉我我该错在哪里或者有其他选择。 任何形式的帮助将不胜感激。 在此先感谢!
您必须从'window.onbeforeunload'返回一个字符串,它将从您无法控制的浏览器中打开一个确认框。如果用户按下取消,导航将被中断。欲了解更多信息,请参阅[这个问题](http://stackoverflow.com/questions/12570800/onbeforeunload-and-onunload-getting-one-to-work-after-the-other/12570865#12570865) – jbabey 2013-04-23 19:37:45