Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back button event capturing in "Ask Question"

The back button event isn't captured by window.onbeforeunload and isn't being handled by the onbeforeunload handler.

There are a few questions on Stack Overflow asking about capturing the back button with no good answer but yet if a user hits the browser's back button while composing a new question on Stack Overflow a message box asking him to if he's sure he wants to continue pops-up.

This message box is different between Chrome and Firefox and feels native.

So, what's behind the curtains?

How can I have the browser popup a message box which gives the user the option to stay on the current page (like with onbeforeunload)?

like image 939
Lior Avatar asked Nov 05 '22 01:11

Lior


1 Answers

The back button event isn't captured by window.onbeforeunload and isn't being handled by the onbeforeunload handler.

Not true. Check this test case with plain JavaScript - back button triggers the onbeforeunload just fine. Checked with recent versions of all major browsers: Chrome 18, Firefox 11 and IE9. (Copy the link address, browse first to some other site then to the fiddle and hit the Back button)

So I fear your question is not really relevant - what you see is the browser native dialog.

like image 131
Shadow Wizard Hates Omicron Avatar answered Nov 09 '22 04:11

Shadow Wizard Hates Omicron