Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we detect browsers refresh and back button events?

Anybody know how to detect browsers refresh and back button events in firefox using jquery or javascript.

like image 567
Manikanta Avatar asked Jan 12 '16 05:01

Manikanta


Video Answer


2 Answers

For back button:

window.addEventListener('popstate', function (event) {
//Your code here
});

For Refresh:

window.onbeforeunload = function () {
 // Your code here
}
like image 64
siva Avatar answered Oct 29 '22 08:10

siva


You can try WindowEventHandlers.onbeforeunload:

window.onbeforeunload = function(e) {

};

and

$(window).unload(function() {
      //
});

Also check Browser Back Button Detection:

I have made a very reusable javascript class, that can be simply dropped into your web page, and when the user clicks back, it will call a function. The default function on this call is a javascript alert “Back Button Clicked”.

To replace this functionality, you simply need to override the OnBack function. This can be done by using the code below.

<script type="text/javascript">
bajb_backdetect.OnBack = function()
{
alert('You clicked it!');
}
</script>

This will now replace the “Back Button Clicked” alert with a “You clicked it!’” alert.

like image 45
Rahul Tripathi Avatar answered Oct 29 '22 08:10

Rahul Tripathi