0

Below is my code to detect and stop the browser's back button in iPad browsers.

$(window).bind("pagehide", function(e) { 
})

How can I stop the page going back on browsers' back button click in iPad browsers?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
techpoint
  • 115
  • 9

1 Answers1

-1

You can use the onbeforeunload event that triggers when a user is leaving your page, whether it's by hitting the back button, entering a new URL or closing the browser.

Here is an example:

window.onbeforeunload = function(){
    return 'You are leaving!?';
}

And here is the result on Chrome:

enter image description here

This event seems to be inconsistent across browsers as some will not support it, some will execute whatever function you pass it, and some will reject the function if it doesn't return a string to put in the confirmation box.

As commenter Alex Wayne stated, think twice about this. It can really create a negative impact on your site or webapp to alter the native behaviour of the back button.

Rémi Breton
  • 4,209
  • 2
  • 22
  • 34
  • 1
    window.onbeforeunload does not work with iPad browsers. Is there any alternative for the same. – techpoint Dec 21 '12 at 04:01
  • The `onunload` event seems to be supported, which I believe is not what you want, but it's the closest to `onbeforeunload` you can get. Check out this answer: http://stackoverflow.com/questions/3239834/window-onbeforeunload-not-working-on-the-ipad#4824156 – Rémi Breton Dec 21 '12 at 14:15
  • link to [window.onbeforeunload does not work with iPad browsers](http://stackoverflow.com/questions/3239834/window-onbeforeunload-not-working-on-the-ipad) – Peter V. Mørch Aug 07 '13 at 12:22