离开网站或关闭浏览器时触发的事件 [英] Event to be triggered when leaving website or closes the browser

查看:656
本文介绍了离开网站或关闭浏览器时触发的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用一段时间执行一个javascript函数,当用户通过在浏览器中键入地址离开网站并点击返回或用户关闭浏览器点击x按钮时。

I´m trying for a while execute a javascript function when a user leaves web site by typing a address in the browser and hits return or the user closes the brower clicking in the 'x' button.

我尝试了卸载事件,但我无法收到警报

I tried the unload event but I cannot get the alert

这就是我正在做的事情:

this is what I am doing:

$(window).unload(function () {
    alert("Are you sure?");
});

$(body).unload(function () {
    alert("Are you sure?");
});

我的想法用完了!

推荐答案

您可以在卸载

$(window).on('beforeunload', function(){
    // ...
})

这篇关于离开网站或关闭浏览器时触发的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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