71
Views
4
Comments
browser back and close button

I want to have a popup when there are unsaved changes in a page and browser back button or close button are clicked. can someone please provide anexample on how to accomplish this? I have searched this forum and almost all of the questions are asking to disable the browser button functionality. 


I want a popup to show which prompts "are you sure?" with yes and no buttons on it.


2021-11-12 04-59-31
Manikandan Sambasivam

Keep track of the unsaved changes in your page using a client variable. For example, you can have a boolean variable called UnsavedChanges that is set to true whenever there are unsaved changes.

Use JavaScript to detect when the user tries to navigate away from the page using the back button or close button. You can do this by handling the beforeunload event.

When the beforeunload event is triggered, show a confirmation dialog using the window.confirm method. This dialog will have "are you sure?" message with yes and no buttons.

// Variable to track unsaved changes

var unsavedChanges = false;

// Function to set unsavedChanges variable

function setUnsavedChanges(value) {

    unsavedChanges = value;

}

// Event handler for beforeunload event

window.addEventListener('beforeunload', function (e) {

    if (unsavedChanges) {

        // Show confirmation dialog

        var confirmationMessage = 'You have unsaved changes. Are you sure you want to leave?';

        e.returnValue = confirmationMessage;

        return confirmationMessage;

    }

});

2024-04-18 08-00-54
Mayur Shrirame
UserImage.jpg
Naina Vadrevu

how do you do it in outsystems?


UserImage.jpg
Naina Vadrevu

@Manikandan Sambasivam - your solution kind of works. it still does redirect the user to the page, i want to stop them from doing that.

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