348
Views
6
Comments
Solved
Confirmation message before close browser tab
Question

Hello everyone! 

I'm trying to throw a confirmation message when the user clicks to close the browser page(the tab). How can I achieve that? 

I've tried this, but without success:


This javascript doesn't work and I don't know why... I hope you can help me! Thanks in advance.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
Solution

Hi Daniel,

Did you check the console for any errors?

EDIT: Did you check this page? The code shown there is a bit more extensive.

2020-04-30 16-28-53
Daniel Almeida

Kilian Hekhuis wrote:

Hi Daniel,

Did you check the console for any errors?

EDIT: Did you check this page? The code shown there is a bit more extensive.


Hi Killian, I used the code provided by this page. It really worked, thank you so much!

But I have a challenge. I have a button at my page, called "Read Confirmation". This button is used by the users to confirm if the news was read. So, the challenge is to throw a confirmation message whenever the user tries to leave the page without press the "Read Confirmation" button.  So, I guess I'll need a variable to store if the user clicked on the button or not...  

Do you have any idea how can I integrate this with the JS code that I already have?  Thanks in advance!



2026-01-19 17-09-56
Carlos Lessa
 
MVP

Daniel Almeida wrote:

Hello everyone! 

I'm trying to throw a confirmation message when the user clicks to close the browser page(the tab). How can I achieve that? 

I've tried this, but without success:


This javascript doesn't work and I don't know why... I hope you can help me! Thanks in advance.

Hi Daniel, this is traditional or reactive? do you have a theme associated with the app? witch one?  Are you interacting with the page when trying to close?

Check this sample is the same as yours but I have the theme based in the Dublin theme. check the oml attached

ps:. You need to interact with the page to work

cheers

Carlos Lessa



AlertClosingApp.oml
2020-04-30 16-28-53
Daniel Almeida

Carlos Lessa wrote:

Daniel Almeida wrote:

Hello everyone! 

I'm trying to throw a confirmation message when the user clicks to close the browser page(the tab). How can I achieve that? 

I've tried this, but without success:


This javascript doesn't work and I don't know why... I hope you can help me! Thanks in advance.

Hi Daniel, this is traditional or reactive? do you have a theme associated with the app? witch one?  Are you interacting with the page when trying to close?

Check this sample is the same as yours but I have the theme based in the Dublin theme. check the oml attached

ps:. You need to interact with the page to work

cheers

Carlos Lessa



Hi Carlos, I'm using Traditional. I have another theme module being consumed by this one. I have a challenge...

I have a button at my page, called "Read Confirmation". This button is used by the users to confirm if the news was read. So, the challenge is to throw a confirmation message whenever the user tries to leave the page without press the "Read Confirmation" button.  So, I guess I'll need a variable to store if the user clicked on the button or not...  

Thank you so much for replying!


2021-03-05 13-56-11
Ricardo Pereira
 
MVP

Hi, 


Can I suggest that you create the function and then create the event listener to use that function?


Something like that:


 

function onBeforeUnload(e) {
    if (thereAreUnsavedChanges()) {
        e.preventDefault();
        e.returnValue = '';
        return;
    }

    delete e['returnValue'];
}

window.addEventListener('beforeunload', onBeforeUnload);

This is just an example.


Best regards, 

Ricardo M Pereira

2020-04-30 16-28-53
Daniel Almeida

Ricardo Pereira wrote:

Hi, 


Can I suggest that you create the function and then create the event listener to use that function?


Something like that:


 

function onBeforeUnload(e) {
    if (thereAreUnsavedChanges()) {
        e.preventDefault();
        e.returnValue = '';
        return;
    }

    delete e['returnValue'];
}

window.addEventListener('beforeunload', onBeforeUnload);

This is just an example.


Best regards, 

Ricardo M Pereira

Ricardo, I guess this function really works too. Thank you so much for helping me!



Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.