onbeforeunload和onunload让另一个工作 [英] onbeforeunload and onunload getting one to work after the other

查看:92
本文介绍了onbeforeunload和onunload让另一个工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在卸载前和卸载方法上遇到了一些麻烦.我以为我仅在onbeforeunload的确认方法设置为true后才根据onload方法设置代码正确.但是遗憾的是事实并非如此,即使onbeforeunload方法设置为false并且该人想留在网站上,卸载方法仍在启动.自从我开始希望它能正常运行以来,这就是我正在处理的代码已发生了很大变化.我确信这不是因为它没有按照我想要的方式工作.

I am having some trouble with the on before unload and the unload method. I thought i set the code right in terms of the onload method only firing up after the confirm method for onbeforeunload was set a true. but sadly that isn't the case, what is happening is the unload method is starting even if the onbeforeunload method is set to false and the person wants to stay at the website. Here is the code I am working on it has changed a lot since I started hope its okay. I am sure it isn't since its not working the way I want it to.

 var validNavigation = false;

 function wireUpEvents() {

     var leave_message = 'Leaving the page ?';

        jQuery(function goodbye() {
jQuery(window).bind('onbeforeunload', function() {
    setTimeout(function() {
        setTimeout(function() {
            jQuery(document.body).css('background-color', 'red');
        }, 10000);
    },1);
    return leave_message;
});
}); 

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

推荐答案

onbeforeunload不能像您认为的那样工作.您可以从处理程序中返回一个字符串,这将提示消息,或者返回undefined则不执行任何操作.

onbeforeunload does not work like you think it does. You can return a string from the handler, which will prompt a messsage, or undefined which will do nothing.

window.onbeforeunload = goodbye;

function goodbye(e) {
    if (!validNavigation) {
        return leave_message;
    } else {
        return undefined;
    }
}

相关:知道的方法如果用户在卸载对话框之前单击Java上的取消"?

这篇关于onbeforeunload和onunload让另一个工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆