如何使用jquery检测页面刷新? [英] How do I detect a page refresh using jquery?

查看:165
本文介绍了如何使用jquery检测页面刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何捕获页面重新加载事件?

How might I capture the page reload event?

我有一个消息系统,当用户刷新页面时,它会丢失所有的输入。我想使用ajax重新填充,因此我需要检测页面何时被刷新/重新加载。

I have a messaging system which loses all its input when the user refreshes the page. I want to use ajax to re-populate, hence my need to detect when the page has been refreshed/reloaded.

推荐答案

$('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?';

});

这篇关于如何使用jquery检测页面刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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