利用localStorage實現對ueditor編輯內容定時儲存為草稿,ueditor草稿箱

來源:互聯網
上載者:User

利用localStorage實現對ueditor編輯內容定時儲存為草稿,ueditor草稿箱

直接看代碼吧

1、引入ueditor和ueditor的使用我就不細說了 詳情請戳http://blog.csdn.net/wangdianyong/article/details/39780709

2、ueditor.jsp


<head>

<!-- 引入jquery -->
<script type="text/javascript" src="js/jQuery2.0.js"></script>
<!-- 引入jquery結束 -->
<!-- 引入ueditor -->
<script type="text/javascript" src="ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="ueditor/ueditor.all.min.js"></script>
<!--引入ueditor結束  -->
<!-- 引入localStorage.js -->
<script type="text/javascript" src="js/localStorage.js"></script>
<!-- 引入localStorage.js結束 -->


<script type="text/javascript">
$(document).ready(function() {
var ue = UE.getEditor('container');
ue.addListener("ready", function() {
// editor準備好之後才可以使用
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="提交">
</form>

</body>


localStorage.js

$(document).ready(
function() {


if (!window.localStorage) {
alert('您的瀏覽器不支援 localStorage 技術!');
} else {
// var spanObj = $("span");
// alert("spanObj" + spanObj);
var saveTimer = setInterval(
function() {
var str = "";
if (document.all) {/* IE */
str = document.frames[1].document.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)) { /*
* 有內容才儲存
* 且有句號或逗號
*/
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 = '(資料儲存於: ' +
// YMDHMS
// + ')';
// var text = $("span").text = '(資料儲存於: '
// + YMDHMS + ')';
$("#span").text('(資料儲存於: ' + YMDHMS + ')');


// alert(text);
setTimeout(function() {
// spanObj.innerText = '';
}, 5000);
}
}, 25000); // 每隔N秒儲存一次
function stoplocs() {
clearInterval(saveTimer); // 停止儲存
// localStorage.removeItem("ctValue"); //清空
}
function showlocs() {
var html = localStorage.getItem("ctValue");
editor.setContent(html);
// alert(localStorage.getItem("ctValue"));
}
}
});


基本的定時儲存為草稿的內容就實現了,你可關閉你的瀏覽器重新開啟頁面發現自己以前編輯的內容並沒有因意外情況的出現而丟失。

原始碼




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.