Is it possible to detect a page refresh (F5) using Jquery [Resolved]

Posted by Sekar.C under jQuery on 8/5/2016 | Points: 10 | Views : 558 | Status : [Member] | Replies : 1
Is it possible to detect a page refresh (F5) using Jquery

How can detect a page is refreshed using Jquery. am tried below code but not working please help me

$(window).bind('beforeunload', function (e) {
//my logiic
});


Regards
Sekar.c



Responses

Posted by: Rajnilari2015 on: 8/7/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
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.

Also refer: http://stackoverflow.com/questions/6741397/is-there-a-way-to-detect-if-the-user-has-pressed-the-refresh-button-using-jquery

Hope that helps

--
Thanks & Regards,
RNA Team

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response