Javascript reminds users of unsaved information when they leave the page (reduce repeated operations) _ javascript tips-js tutorial

Source: Internet
Author: User
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:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.