且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jquery检测页面刷新?

更新时间:2023-12-02 11:09:52

$('body').bind('beforeunload',function(){
   //do something
});

但是,这不会保存任何信息以供以后使用,除非您计划将其保存在cookie的某个地方或本地存储),并且卸载事件并不总是在所有浏览器中触发。

But this wont save any info for later, unless you were planning on saving that in a cookie somewhere (or local storage) and the unload event does not always fire in all browsers.

示例: http://jsfiddle.net/maniator/qpK7Y/

代码:

$(window).bind('beforeunload',function(){

     //save info somewhere

    return 'are you sure you want to leave?';

});