- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Alert for unsaved changes in form in JavaScript
Following is the code to display an alert for form’s unsaved changes in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } input { margin: 10px; } </style> </head> <body onbeforeunload="return pageUnload()"> <h1>Alert for unsaved changes in form</h1> <form> Username <input type="text" /> <br /> Password <input type="password" /><br /> <button type="submit">SUBMIT</button> </form> <br /> <script> function pageUnload() { return "The data on this page will be lost if you leave"; } </script> </body> </html>
Output
If we click on the reload button then the following warning will be shown −
- Related Articles
- Trigger a button click and generate alert on form submission in JavaScript
- How to restore unsaved file in Excel?
- What is an alert box in JavaScript?
- How to show hyperlinks in JavaScript alert?
- Explain JavaScript text alert
- How to provide new line in JavaScript alert box?
- How to show image in alert box using JavaScript?
- How to display JavaScript variable value in alert box?
- How do I display image in alert/confirm box in JavaScript?
- How to change button label in alert box using JavaScript?
- How I can replace a JavaScript alert pop up with a fancy alert box?
- How to Line Breaks to JavaScript Alert?
- How to change the color of the alert box in JavaScript?
- Methods for tracking database schema changes in MySQL?
- How to edit a JavaScript alert box title?

Advertisements