AutoSave/Automatic Storage

Source: Internet
Author: User

From: http://www.fayland.org/journal/AutoSave.html

This function is common. It is to prevent the loss of things that have worked so hard to write due to browser crash or failed submission. This is also in Gmail.
The principle is to store the content in the text box into a Cookie. If it is not submitted successfully (probably because the browser crashes), the next time you access the page, you will be asked whether to import the last stored content.
Function AutoSave (it) {// it indicates the called text box
Var _ value = it. value; // obtain the value of the text box.
If (! _ Value ){
Var _ LastContent = GetCookie ('autosavecontent'); // obtain the cookie value. Here, GetCookie is a custom function. For more information, see the source code.

If (! _ LastContent) return; // If the cookie has no value, it indicates it is a new start.

If (confirm ("Load Last AutoSave Content? ") {// Otherwise, ask whether to import
It. value = _ LastContent;
Return true;
}
} Else {

Var expDays = 30;
Var exp = new Date ();
Exp. setTime (exp. getTime () + (expDays * 86400000); // 24*60*60*1000 = 86400000
Var expires = '; expires =' + exp. toGMTString ();

// SetCookie: set this cookie.
Document. cookie = "AutoSaveContent =" + escape (_ value) + expires;
}
}

This should be the case in HTML:


<script language=JavaScript src='/javascript/AutoSave.js'></script>
<form action="submit" method="POST" onSubmit="DeleteCookie('AutoSaveContent')">
<textarea rows="5" cols="70" wrap="virtual" onkeyup="AutoSave(this);" onselect="AutoSave(this);" onclick="AutoSave(this);"></textarea>
<input type="submit"></form>

The first statement imports js, and the second sentence's onSubmit indicates that the cookie will be deleted if it is submitted, and the DeleteCookie is also a custom function. See source code.
In textarea, onkeyup is used to access AutoSave when a key is clicked to store newly written text.
Onselect and onclick are used to determine the automatically saved text during the new access.

This is generally the case. Enjoy!

Source code: http://www.fayland.org/javascript/AutoSave.js

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.