I'm building a website with a responsive design. On mobile devices, many sections of the page are hidden in "drawers", which can be revealed with a button.
Because of the close resemblance to native phone apps, I have noticed that some users use their device's back button to close the drawers, but the back button instructs the browser to go back to the previous page.
Is there a way to intercept this? Preferably on mobile devices only, as I have no intend on changing the browser's back button behavior across the board.
I'm thinking something like:
// call this code when back button was pressed
if( $( '.drawer' ).hasClass( 'open' ) ) {
// prevent browser going back a page
$( '.drawer' ).removeClass( 'open' );
} else {
// keep normal browser behavior
}
Can it be done?