Reload alert?

I’ve been trying to make an alert that lets the user know
that if they close the window ( window.close() ) their
progress might be lost. I was able to get the alert working
and all, but when testing it, and clicking “cancel” the page
would still reload, and my progress would be lost. On the
other hand, the code I have below is how I expect it to work.
It’s difficult to test this action with Wick editor itself since Wick
has its own alert when you reload the page that only works
when your not in the project testing mode of Wick. I’d greatly
appreciate an answer of any type, thanks in advance.

onEvent('window.close()', function () {
  if(confirm('Alert message')){
        window.location.reload(true);  
    }else{
        window.location.reload(false);
}
});
1 Like

I’m not sure this’ll work, but try the same thing but without the else clause:

onEvent(‘window.close()’, function () {
if(confirm(‘Alert message’)){
window.location.reload(true);
}
});

Thanks for the attempt @nick, I tried it on Wick, and it didn’t work.

1 Like

Hi @Hamzah_Al_Ani

If you use ‘window.close’ you are basically too late - that is the call the window is closing.
Use beforeunload uinstead:

<script language="JavaScript">
    window.onbeforeunload = confirmExit;
    function confirmExit() {
        return "You have attempted to leave this page. Are you sure?";
    }
</script>

This way, you can prevent the closing from happening.
That should work…

regards,

Paul

It works, but the return function doesn’t seem to work correctly. It just says “changes might not be saved” or something like that.

Wick itself has it’s own alert, when I tried it as an html, it didn’t work.
Thanks for the attempt, Paul.

I’ll try to use beforeunload in all ways, and I’ll let you guys know if I succeed.