if you want to  detect if a user has clicked back button or not. For this you can simply use this code 
window.onbeforeunload = function (e) {
}
It works if a user clicks back button. But this event is also fired if a user click F5 or reload button of browser. How do I fix this? you can simply use the below code for this ….
you can try this (if the browser does not support “onbeforeunload”):
jQuery(document).ready(function($) {

if (window.history && window.history.pushState) {

$
(window).on('popstate', function() {
var hashLocation = location.hash;
var hashSplit = hashLocation.split("#!/");
var hashName = hashSplit[1];

if (hashName !== '') {
var hash = window.location.hash;
if (hash === '') {
alert
('Back button was pressed.');
}
}
});

window
.history.pushState('forward', null, './#forward');
}

});