防止用户将我的页面留在js中 [英] Prevent user to leave my page in js

查看:62
本文介绍了防止用户将我的页面留在js中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表单中,我有一个未显示的模式.如果用户单击我的离开图像",我打开模态,然后询问他是否要离开.

In my form i have an unshow modal. I open the modal if the user click on my "leave image" and i ask if he want to leave.

如果用户说是",是否可以在JavaScript(Jquery)中停止"重新加载或页面更改以显示我的模态并继续操作(重新加载,下一页,上一页等)?

Is that possible in JavaScript (Jquery ?) to "stop" the reloading or the page change to show my modal and continue the action (Reload, Next page, Prev page, etc...) if the user say yes ?

我尝试过:

$(window).bind('beforeunload', function(){
    return 'message to show';
});

但是我不想打开浏览器弹出窗口,我想要我的^^.

But i don't want to open the browser popup, i want mine ^^.

推荐答案

同时存在window.onbeforeunloadwindow.onunload,它们的使用因浏览器而异.您可以通过将窗口属性设置为功能或使用.addEventListener:

There is both window.onbeforeunload and window.onunload, which are used differently depending on the browser. You can ask them either by setting the window properties to functions, or using the .addEventListener:

window.onbeforeunload = function(){
   // Do something
}

//或

window.addEventListener("beforeunload", function(e){
   // Do something
}, false);

通常,如果需要停止用户离开页面,则使用onbeforeunload(例如,用户正在处理某些未保存的数据,因此他/她应在离开之前进行保存).据我所知,Opera不支持onunload,但您始终可以同时设置两者.

Usually, onbeforeunload is used if you need to stop the user from leaving the page (ex. the user is working on some unsaved data, so he/she should save before leaving). onunload isn't supported by Opera, as far as I know, but you could always set both.

这篇关于防止用户将我的页面留在js中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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