Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect a page refresh using jquery?

How might I capture the page reload event?

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.

like image 614
sisko Avatar asked Nov 04 '11 17:11

sisko


People also ask

How do you know if a page is refreshed?

A better way to know that the page is actually reloaded is to use the navigator object that is supported by most modern browsers. It uses the Navigation Timing API. thanks buddy this is accurate solution to detect either the page is reloaded from right click/refresh from the browser or reloaded by the url.

What is the difference between browser close and refresh?

When we refresh the page (F5, or icon in browser), it will first trigger ONUNLOAD event. When we close the browser (X on right top icon),It will trigger ONUNLOAD event.

How can I tell if ASP NET is refreshing a page?

In the 'page_Load' event we check if the hidden field value is same as the old value. In case the value is not same that means it's a 'postback' and if the value is same then its 'refresh'. As per situation we set the 'httpContent. Items["Refresh"]' value.


1 Answers

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

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.


Example: http://jsfiddle.net/maniator/qpK7Y/

Code:

$(window).bind('beforeunload',function(){       //save info somewhere      return 'are you sure you want to leave?';  }); 
like image 148
Naftali Avatar answered Sep 18 '22 11:09

Naftali