When you leave the page, determine whether there are unsaved input values, and then remind you. Next we will introduce the implementation steps. If you are interested, you can learn more.
Javascript code
The Code is as follows:
// Determine whether there are unsaved input values when you leave the page
Var hasSaved = false; // whether the flag is not saved for the input content. The initial value is false.
Function CheckUnsave (){
If (hasSaved = false ){
Alert ("your uploaded items have not been saved; save them before leaving the page ");
Return false ;}
// Return true; // This statement cannot be added
}
// If it is saved, the status changes.
Function ChangeState ()
{
HasSaved = true;
}
Script
Add the onbeforeunload attribute to the body.
Html code
The Code is as follows:
Call ChangeState before submitting the action.
If you exit the page after pressing a button, for example, the Save button, you need to modify the status of the saved flag after pressing the Save button. For example:
Html code
The Code is as follows: