Conditional onbeforeunload event in a page

My quick example of the conditional prompt before leaving page:

    window.onbeforeunload = confirmExit;
    function confirmExit(event) {
        var messageText = tinymce.get('mMessageBody').getContent();
        messageText = messageText.trim();

        // ... whatever you want

        if (messageText != "")
            return true;
        else
            return void (0);
   };

It works under Chrome, FF.


$(window).bind('beforeunload',function() {
    return "'Are you sure you want to leave the page. All data will be lost!";
});

$('#a_exit').live('click',function() {
    $(window).unbind('beforeunload');
});

Above works for me