autosave/Automatic storage function realizes _javascript skill

Source: Internet
Author: User

Turn from: http://www.fayland.org/journal/AutoSave.html

This feature is very common. is to prevent the browser from crashing or submitting unsuccessful and the things that cause you to write away. That's the same thing in Gmail.
It works by storing the contents of the text box into a Cookie. If the failure to commit succeeds (possibly because of a browser crash), the next time you visit the page, ask if you want to import the last thing that was stored.
function AutoSave (IT) {//It refers to the text box called
var _value = It.value; Get the value of a text box
if (!_value) {
var _lastcontent = GetCookie (' autosavecontent '); Get the value of the cookie, here's GetCookie is a custom function, see source code

if (!_lastcontent) return; If the cookie does not have a value, the description 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 This is where you set this cookie
Document.cookie = "autosavecontent=" + Escape (_value) + expires;
}
}

And 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= "wrap=" virtual "onkeyup=" AutoSave (This), "onselect=" AutoSave (this); "onclick=" AutoSave (this); " ></textarea>
<input type= "Submit" ></form>
The first sentence to import JS, the second sentence of OnSubmit refers to the deletion of the cookie if committed, and Deletecookie is also a custom function. See source code.
The onkeyup in textarea refers to accessing the AutoSave when the key is pressed to store the newly written text.
Onselect and onclick are used to determine the text that is automatically saved when you use the new access.

This is roughly 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.