Custom closing operation on javascript [duplicate]

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


Solution 1:

That's not possible in JavaScript because that would be a big security problem. Every browser has its own beforeunload dialog style and you can't customize it.

W3Schools says:

The default message that appears in the confirmation box, is different in different browsers. However, the standard message is something like "Are you sure you want to leave this page?". This message cannot be removed.