0

I'm having a simple html page, which registers a listener on navigate that's there from JQuery-Mobile. But I can't see the callback getting executed.

The following is the html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
        <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    </head>
    <body>
        something
        <script>
            $( window ).on('navigate', function( event, data ){
                if ( data.state.direction == "back" ) {
                    console.log('back button pressed');
                }
                if ( data.state.direction == "forward" ) {
                    console.log('fwd button pressed');
                }
            });
        </script>
    </body>
</html>

PS: from here I got to know that JQuery mobile don't support JQuery 3.x, so I changed it to 1.x, but still this don't help.

Whenever I press the back button (when I'm on the above html page), I suppose I should get `` on the console. But that's not happening. Is there anything obvious that I'm missing?

Lavish Kothari
  • 2,211
  • 21
  • 29
  • Have you tried using the "beforeunload" event instead of "navigate"? Not sure if that would be about the same. – asimovwasright Aug 06 '19 at 09:34
  • @asimovwasright If I use `beforeunload`, then I won't be able to detect the direction, whether it's forward or back. – Lavish Kothari Aug 06 '19 at 09:38
  • Ahh I see. In that case, maybe you could use a popstate handler rather - https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event - which will be able to tell when the history has changed, and do some basic logic on the original history to tell back from forward. – asimovwasright Aug 06 '19 at 09:53
  • Thanks for the suggestion over the `popstate` event. I tried that. But I'd not like to go with it as it distorts the browser's history and eventually ruins the user-experience. (`popstate` event is triggered only when navigation happens between two history entries for the same document, so I'll have to call a `pushState` which will distort browser's history) – Lavish Kothari Aug 06 '19 at 09:57
  • Fair enough :-) In that case, good luck! – asimovwasright Aug 06 '19 at 09:57

0 Answers0