Use localStorage to regularly save the edited content of ueditor as a draft.

Source: Internet
Author: User

Use localStorage to regularly save the edited content of ueditor as a draft.

Check the Code directly.

1, the introduction of ueditor and ueditor use I will not elaborate on the details please stamp http://blog.csdn.net/wangdianyong/article/details/39780709

2. ueditor. jsp


<Head>

<! -- Introduce jquery -->
<Script type = "text/javascript" src = "js/jQuery2.0.js"> </script>
<! -- End with jquery introduction -->
<! -- Introduce ueditor -->
<Script type = "text/javascript" src = "ueditor/ueditor. config. js"> </script>
<Script type = "text/javascript" src = "ueditor/ueditor. all. min. js"> </script>
<! -- End with ueditor introduction -->
<! -- Introduce localStorage. js -->
<Script type = "text/javascript" src = "js/localStorage. js"> </script>
<! -- Import localStorage. js to end -->


<Script type = "text/javascript">
$ (Document). ready (function (){
Var ue = UE. getEditor ('Container ');
Ue. addListener ("ready", function (){
// The editor is ready for use.
Var html = localStorage. getItem ("ctValue ");
Alert (html );
Ue. setContent (html );


});


});
</Script>
</Head>


<Body>

$ {Param. content}
<P>
<Span id = "span" name = "span"> </span>
</P>
<Form action = "ueditor. jsp" method = "post">


<Script id = "container" name = "content" type = "text/plain"
Style = "width: 800px; height: 400px;">
</Script>


<Script type = "text/javascript">
Var ue = UE. getEditor ('Container ');
</Script>
<Input type = "submit" value = "submit">
</Form>

</Body>


LocalStorage. js

$ (Document). ready (
Function (){


If (! Window. localStorage ){
Alert ('your browser does not support localStorage technology! ');
} Else {
// Var spanObj = $ ("span ");
// Alert ("spanObj" + spanObj );
Var saveTimer = setInterval (
Function (){
Var str = "";
If (document. all) {/* IE */
Str = document.frames[12.16.doc ument. body. innerHTML;
} Else {/* Chrome, ff */
// Str =
// Document. getElementById ("container"). contentDocument. body. innerHTML;
Var ue = UE. getEditor ('Container ');
Str = ue. getContent ();
}
If (str. length> 20
& (Str. indexOf (". ")>-1 | str
. IndexOf (",")>-1 )){/*
* Content is saved
* There are periods or commas (,).
*/
LocalStorage. setItem ("ctValue", str );
Var d = new Date ();
Var YMDHMS = d. getFullYear () + "-"
+ (D. getMonth () + 1) + "-"
+ D. getDate () + ""
+ D. getHours () + ":"
+ D. getMinutes () + ":"
+ D. getSeconds ();
// Alert (YMDHMS );
// SpanObj. innerHTML = '(data is stored in:' +
// YMDHMS
// + ')';
// Var text = $ ("span"). text = '(Save the data :'
// + YMDHMS + ')';
$ ("# Span"). text ('(data is stored in' + YMDHMS + ')');


// Alert (text );
SetTimeout (function (){
// SpanObj. innerText = '';
},5000 );
}
}, 25000); // save every N seconds
Function stoplocs (){
ClearInterval (saveTimer); // stop saving
// LocalStorage. removeItem ("ctValue"); // clear
}
Function showlocs (){
Var html = localStorage. getItem ("ctValue ");
Editor. setContent (html );
// Alert (localStorage. getItem ("ctValue "));
}
}
});


You can close your browser and re-open the page to find that the previously edited content is not lost due to unexpected situations.

Source code




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.