It happens to me quite often on FaceBook, because I often change my mind about posting things that I have started to write.
As far as I remember, the browser doesn't react when you leave the page by closing the tab, but if you try to leave the page by bringing a new page up within the same tab with something written in a field but not posted to the server you get the "do you want to" message.