如何在JS中停止卸载页面(导航)? [英] How do I stop a page from unloading (navigating away) in JS?

查看:96
本文介绍了如何在JS中停止卸载页面(导航)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何阻止页面重新加载或导航?

 jQuery(function($){

/ *全局卸载通知* /
warning = true;

if(warning){
$(window).bind(unload,function(){
if(confirm(你想离开这个页面)== true){
//他们按下OK
alert('ok');
} else {
//他们按取消
alert('cancel');
return false;
}
});
}
}); / pre>

我正在一个电子商务网站上工作,显示您未来订单的页面可以使用+ / - 纽扣。以这种方式更改数量并不会实际改变订单本身,他们必须按确认,因此提出一个积极的措施来更改订单。



但是如果他们有更改数量并从页面导航,我想提醒他们他们正在这样做,以防万一这是一个意外,因为更改的数量将会丢失,如果他们导航或刷新页面。



在上面的代码中,我使用的全局变量默认为false(它唯一适用于测试),当数量更改时,我将更新此变量为true,当它们确认我会将其设置为false。



如果警告是正确的,页面被卸载,我给他们一个确认框,如果他们说不想留在这个页面上,我需要停止它卸货。返回false不起作用,它仍然允许用户导航(提醒仅供调试使用)



任何想法?

解决方案

onbeforeunload 是你想要的;您的函数应该为Event对象的returnValue属性分配一个字符串值,并返回相同的字符串。检查 Microsoft Mozilla 的详细信息。



字符串您的返回将被浏览器用于向用户显示一个自定义确认框,允许他们拒绝留在那里,如果他们这样选择。必须这样做才能防止恶意脚本导致拒绝浏览器的攻击。<​​/ p>

Does anyone know how to stop a page from reloading or navigating away?

jQuery(function($) {

    /* global on unload notification */
    warning = true;

    if(warning) {
        $(window).bind("unload", function() { 
            if (confirm("Do you want to leave this page") == true) {
                //they pressed OK
                alert('ok');
            } else {
                // they pressed Cancel
                alert('cancel');
                return false;
            }
        });
    }
});

I am working on an e-commerce site at the moment, the page that displays your future orders has the ability to alter the quantities of items ordered using +/- buttons. Changing the quantities this way this doesn't actually change the order itself, they have to press confirm and therefore committing a positive action to change the order.

However if they have changed the quantities and navigate away from the page I would like to warn them they are doing so in case this is an accident, as the changed quantities will be lost if they navigate away or refresh the page.

In the code above I am using a global variable which will be false by default (its only true for testing), when a quantity is changed I will update this variable to be true, and when they confirm the changes I will set it to false.

If warning is true and the page is unloaded, I offer them a confirmation box, if they say no they would like to stay on this page I need to stop it from unloading. return false isn't working, it still lets the user navigate away (the alerts are there for debugging only)

Any ideas?

解决方案

onbeforeunload is the one you want; your function "should assign a string value to the returnValue property of the Event object and return the same string". Check the docs from Microsoft and Mozilla for details.

The string you return will be used by the browser to present the user with a custom confirm box, allowing them to refuse to stay there if they so choose. It has to be done that way to prevent malicious scripts causing a Denial-of-Browser attack.

这篇关于如何在JS中停止卸载页面(导航)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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