Open In App

How to display warning before leaving the web page with unsaved changes using JavaScript ?

To display a warning before leaving a web page with unsaved changes, you can use the beforeunload event in JavaScript.

Syntax:

// Event listener for the 'beforeunload' event
window.addEventListener('beforeunload', function (e) {
// Check if any of the input fields are filled
if (fname !== '' || lname !== '' || subject !== '') {
// Cancel the event and show alert that
// the unsaved changes would be lost
e.preventDefault();
e.returnValue = '';
}
});

Approach

Example: This example displays a warning before leaving the web page with unsaved changes using JavaScript.






// Variables to store the input text
let fname = '';
let lname = '';
let subject = '';
 
// Functions to update the input text
updateFname = () => {
    fname = document
        .getElementById('fname').value;
}
 
updateLname = () => {
    lname = document
        .getElementById('lname').value;
}
 
updateSubject = () => {
    subject = document
        .getElementById('subject').value;
}
 
// Event listener for the
// 'beforeunload' event
window.addEventListener('beforeunload',
    function (e) {
 
        // Check if any of the input
        // fields are filled
        if (fname !== '' ||
            lname !== '' ||
            subject !== '') {
 
            // Cancel the event and
            // show alert that the unsaved
            // changes would be lost
            e.preventDefault();
            e.returnValue = '';
        }
    });




<!DOCTYPE html>
<html>
<body>
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
 
    <p>
        The page will notify if the user has
        started filling the form and tries
        to navigate away from it.
    </p>
 
    <div class="container">
        <h2>A Demo Contact Form</h2>
        <form action="#">
            <label>First Name</label>
 
            <!-- Create all the input boxes and
            assign their respective functions
            to update the content in a variable -->
            <input type="text"
                   id="fname"
                   name="firstname"
                   onchange="updateFname()">
 
            <label>Last Name</label>
            <input type="text"
                   id="lname"
                   name="lastname"
                   onchange="updateLname()">
 
            <label>Subject</label>
            <textarea id="subject"
                      name="subject"
                      style="height:100px"
                      onchange="updateSubject()">
            </textarea>
 
            <button class="submit-btn"
                    onclick="return false;">
                Submit
            </button>
        </form>
    </div>
</body>
</html>




.container {
    border: 2px dotted;
    width: 60%;
    border-radius: 5px;
    padding: 10px;
}
 
input,
textarea {
    width: 90%;
    padding: 10px;
    margin: 10px;
}
 
.submit-btn {
    background-color: green;
    color: white;
    padding: 10px;
}

Output: Click here to check the live output.




Article Tags :