Code for regularly saving form data in javascript

Source: Internet
Author: User

(Forget whether both mailboxes have this function ).
How does this function work?

Timing, we know how to get it, but what about saving it? Maybe we can store data by hiding domains and other means. However, this has a disadvantage: After the page is refreshed, data will be lost.
At this time, it is our turn to focus little on, and it is estimated that many people do not know the UserData Behavior (userData Behavior) debut:
What is this UserData? How to use it ?, I will repost an article about it at the end of the article.
Now, let's look at the example: no code, no truth:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> new document </title>
<Meta name = "generator" content = "editplus"/>
<Meta name = "author" content = ""/>
<Meta name = "keywords" content = ""/>
<Meta name = "description" content = ""/>
<Script type = "text/javascript">

Window. onload = function (){

Var txtObj = document. getElementById ('txt1 ');
Var spanObj = document. getElementById ('s1 ');

// Automatically save
TxtObj. addBehavior ("# default # userData ");

Var saveTimer = setInterval (function (){
TxtObj. setAttribute ('ovalue', txtObj. value );
TxtObj. save ('saveddata ');

SpanObj. innerText = 'data is stored in: '+ (new Date ());
SetTimeout (function (){
SpanObj. innerText = '';
},1000 );

}, 10000); // save every minute

Document. getElementById ('btn1 '). attachEvent ('onclick', function (){
ClearInterval (saveTimer); // cancel saving
TxtObj. removeAttribute ('ovalue ');
});

Document. getElementById ('btn2'). attachEvent ('onclick', function (){
TxtObj. load ('saveddata ');
Alert (txtObj. getAttribute ('ovalue '));
// TxtObj. value = txtObj. getAttribute ('ovalue ');
});
};

</Script>
</Head>

<Body>
<Span id = "s1" style = "color: red;"> </span>
<P/>
<Textarea height = "500" style = "height: 500px; width: 500px;" id = "txt1">
</Textarea>
<P/>
<Input type = "button" id = "btn1" value = "Stop saving"/>
<Input type = "button" id = "btn2" value = "get saved value"/>
</Body>
</Html>

Copy the html section and run it. In fact, this is basically the same as the regular saving in the mailbox. In this case, it's okay to polish it.
Let's take a look at this article, which uses userData to save form data on the client.

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.