在我的网站中,我在新窗口中进行了考试,我希望如果用户关闭此窗口,如果他在确认时按“确定”,他将被重定向到其他页面。但如果按下“取消”,他应该留在那里。我正在使用的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”窗口会成功打开,但这里的问题是,如果用户在确认框中单击“取消”,那么窗口也会关闭。请告诉我我哪里出错了或任何替代方法。任何形式的任何帮助将不胜感激。提前致谢!!