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.
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.
All the code is client side, I hope you fine this helpful:
First thing there are 3 functions we will use:
Now we will start with the page load:
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:
if you want to bookkeep some variable before page refresh
if you want o load some content base on some condition
There are two events on client side as given below.
1. window.onbeforeunload (calls on Browser/tab Close & Page Load)
2. window.onload (calls on Page Load)
On Browser/Tab Close: if user close the Browser/tab, then window.onbeforeunload will fire and IsRefresh value on server side will be "Close".
On Refresh/Reload/F5: If user will refresh the page, first window.onbeforeunload will fire with IsRefresh value = "Close" and then window.onload will fire with IsRefresh value = "Load", so now you can determine at last that your page is refreshing.