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.
$('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?';
});
If you want to keep some variable before page was refreshed:
$(window).on('beforeunload', function(){
// your logic here
});
If you want to load some content based on some condition:
$(window).on('load', function(){
// your logic here
});
All the code is client side, I hope you fine this helpful:
First thing there are 3 functions we will use:
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function DeleteCookie(name) {
document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}
Now we will start with the page load:
$(window).load(function () {
//if IsRefresh cookie exists
var IsRefresh = getCookie("IsRefresh");
if (IsRefresh != null && IsRefresh != "") {
//cookie exists then you refreshed this page(F5, reload button or right click and reload)
//SOME CODE
DeleteCookie("IsRefresh");
}
else {
//cookie doesnt exists then you landed on this page
//SOME CODE
setCookie("IsRefresh", "true", 1);
}
})
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 server Side
public JsonResult TestAjax( string IsRefresh)
{
JsonResult result = new JsonResult();
return result = Json("Called", JsonRequestBehavior.AllowGet);
}
On Client Side
<script type="text/javascript">
window.onbeforeunload = function (e) {
$.ajax({
type: 'GET',
async: false,
url: '/Home/TestAjax',
data: { IsRefresh: 'Close' }
});
};
window.onload = function (e) {
$.ajax({
type: 'GET',
async: false,
url: '/Home/TestAjax',
data: {IsRefresh:'Load'}
});
};
</script>
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.