While using Facebook on the latest Chrome browser on my Mac, I observed an interesting behavior. When I began typing in a comment box and then clicked on another link or pressed the back button, a confirmation window popped up asking if I wanted to leave:
As a seasoned developer, I always thought that the only way to handle such situations was by attaching an onbeforeunload
event to the page. I knew you could simulate onbeforeunload
with a global binding on anchors, but I was surprised to see Facebook's custom-styled confirmation instead of the usual bland one.
I wonder how they achieved this. Could it be related to HTML5 window history or pushState events?