我需要捕获浏览器事件,例如刷新,关闭,后退按钮按下。如果发生任何这些事件,我需要重定向到Login.aspx页面。我尝试过以下代码,但它也不适用于弹出窗口。
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = function(e) {
return 'Are you sure you want to leave this page? You will lose any unsaved data.';
};
</script>
在刷新或按下按钮或按下浏览器按钮时,我需要做什么才能重定向到Login.aspx页面?
答案 0 :(得分:0)
这可以帮到你。
window.onbeforeunload = function () {
window.setTimeout(function () { // escape function context
window.location = 'Login.aspx';
}, 0);
window.onbeforeunload = null; // necessary to prevent infinite loop
// that kills your browser
return 'Press "Stay On Page" to go to current page!';
// pressing leave will still leave, but the GET may be fired first anyway
}