Customize exit operation when closing tab or browser

Hi i’m trying to customize the operation made when a tab/browser is closed on my software.
I’ve found a script that stop the execution of the closing operation and display a popup that ask to confirm the exit intent:

<script type="text/javascript">
    window.addEventListener('beforeunload', function(e) {
        e.preventDefault();
        e.returnValue='';
    });
</script>

I want to make this script execute other operation like showing a confirm dialog, but cant figure out how to make it. I want something like

<script type="text/javascript">
    window.addEventListener('beforeunload', function(e) {
        e.stopClosing();
        //Show confirm dialog
    });
</script>

I’m using Primefaces and JSF. Thanks