强制用户访问浏览器并执行对话框操作



我正在尝试在 Web 应用程序中实现服务器超时警报。我按照这里给出的说明进行操作。它工作正常。我正在寻找一种方法,当弹出超时对话框时,它应该从他/她所在的窗口/浏览器重定向用户并在对话框中执行操作。任何线索都是有帮助的。谢谢:)

这是我的代码片段:

<script>
    var myRedirectUrl = '/logout/';
    var idleTime = 5000 //read session timeout value from web.config number of miliseconds until the user is considered idle
    var initialSessionTimeoutMessage = 'Your session will expire in <spanid="sessionTimeoutCountdown"></span> seconds.<br>Click on <b>OK</b> to continue your session.';
    var sessionTimeoutCountdownId = 'sessionTimeoutCountdown';
    var redirectAfter = 10 //read session timeout value from web.config
    var redirectAfterPause = 2000; //# of milliseconds to show expiredMessage before redirting to redirectTo
    var redirectTo = myRedirectUrl; // URL to relocate the user to once they have timed out
    var keepAliveURL = '/'; // URL to call to keep the session alive
    var expiredMessage = 'Your session has expired.  You are being logged out for security reasons.'; // message to show user when the countdown reaches 0
    var running = false; // var to check if the countdown is running
    var timer; // reference to the setInterval timer so it can be stopped
    var stayButton = 'Continue' //Text on button to stay on current page
    var leaveButton = 'Exit' //text on button to take user to the redirectTo page
    $(document).ready(function () {
            // create the warning window and set autoOpen to false
            var sessionTimeoutWarningDialog = $("#sessionTimeoutWarning");
            $(sessionTimeoutWarningDialog).html(initialSessionTimeoutMessage);
            $(sessionTimeoutWarningDialog).dialog({
                    title: 'Session Expiration Warning',
                    autoOpen: false,  // set this to false so we can manually open it
                    closeOnEscape: false,
                    draggable: false,
                    width: 460,
                    minHeight: 50,
                    modal: true,
                    beforeClose: function () { // bind to beforeclose so if the user clicks on the "X" or escape to close the dialog, it will work too
                            // stop the timer
                            clearInterval(timer);
                            // stop countdown
                            running = false;
                            // ajax call to keep the server-side session alive
                            $.ajax({
                                    url: keepAliveURL,
                                    async: false
                            });
                    },
                    buttons: {
                            'keep-alive-button' : {
                                    text: stayButton,
                                    click : function () {
                                            // close dialog
                                            $(this).dialog('close');
                                    }
                            },
                            'sign-out-button' : {
                                    text: leaveButton,
                                    click : function () {
                                            // close dialog
                                            window.location = redirectTo;
                                    }
                            }
                    },
                    resizable: false,
                    open: function () {
                                    // scrollbar fix for IE
                                    $('body').css('overflow', 'hidden');
                    },
                    close: function () {
                                    // reset overflow
                                    $('body').css('overflow', 'auto');
                    }
            }); // end of dialog

            // start the idle timer
            $.idleTimer(idleTime);
            // bind to idleTimer's idle.idleTimer event
            $(document).bind("idle.idleTimer", function () {
                    // if the user is idle and a countdown isn't already running
                    if ($.data(document,'idleTimerObj').idle === true && !running) {
                            var counter = redirectAfter;
                            running = true;
                            // intialisze timer
                            $('#' + sessionTimeoutCountdownId).html(redirectAfter);
                            // open dialog
                            $(sessionTimeoutWarningDialog).dialog('open');
                            // create a timer that runs every second
                            timer = setInterval(function () {
                                    counter -= 1;
                                    // if the counter is 0, redirect the user
                                    if (counter === 0) {
                                            $(sessionTimeoutWarningDialog).html(expiredMessage);
                                            setTimeout(function () {
                                                    $(sessionTimeoutWarningDialog).dialog('disable');
                                                    window.location = redirectTo;
                                            }, redirectAfterPause);
                                    } else {
                                            $('#' + sessionTimeoutCountdownId).html(counter);
                                    };
                            }, 1000);
                    };
            });
    });

</script>

在 HTML 文件中添加:

<div id="sessionTimeoutWarning" style="display: none"></div>

这将打开对话框,但不会强制用户访问该页面。因此,会话可能会在用户没有意识到的情况下过期。如何避免这种行为?

试试这个window.parent.location

编辑

    $(sessionTimeoutWarningDialog).dialog('disable');
           window.parent.location = redirectTo; //change it here
    }, redirectAfterPause);

由于您想通过pop up重定向页面,这将帮助您定位pop up's父级,这是基本window

最新更新