Could someone please share experience / code how we can detect the browser back button click (for any type of browsers)?
We need to cater all browser that doesn't support HTML5
One option is to use jquery mobile. According to this source, to detect the 'back' key, KEYCODE_BACK = 4 on Android.
The 'popstate' event only works when you push something before. So you have to do something like this:
jQuery(document).ready(function($) { if (window.history && window.history.pushState) { window.history.pushState('forward', null, './#forward'); $(window).on('popstate', function() { alert('Back button was pressed.'); }); } });
For browser backward compatibility I recommend: history.js
In javascript, navigation type 2 means browser's back or forward button clicked and the browser is actually taking content from cache.
if(performance.navigation.type == 2) { //Do your code here }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With