All the beforeunload
handler is really supposed to do is return a string, which the browser then displays in an "are-you-sure-you-want-to-leave" dialog. If the user clicks OK, whatever navigation was about to happen occurs; if they click Cancel, it doesn't. Take a look here for more detail.
The usual thing to do here would be to display a message (by returning a string, not calling confirm
yourself) along this lines of "You're about to lose the changes you've made to the current layout; are you sure you want to leave?" and then let the user themselves click Cancel and then Save, or OK if they don't care.
You're having issues, I expect, because you're trying to perform a postback in the handler, and the postback itself would cause an unload. I wouldn't be surprised if the browser deliberately stops this kind of behaviour in the handler, because malicious sites could use it to stop you leaving.
As per your update: only some browsers even allow beforeunoad
behaviour. By the looks of it, Firefox does, so you get two dialogs - your confirm
and then the browser default one. And by the looks of it, Chrome doesn't, so your event never gets called. (Or maybe Chrome just ignores the event if it does something unexpected, like post back.)