js定时保存草稿(js设置cookie和ajax通过后台设置cookie)(包含获取kindeditor中的textarea的值)

setTimeout("daojishi()", 1000);

function daojishi() {

shijian = document.getElementById('timing').innerHTML;

document.getElementById('timing').innerHTML = parseInt(shijian) - 1;

if (parseInt(shijian) - 1 < 0) {

save_outline();

document.getElementById('timing').innerHTML = 30;

}

setTimeout("daojishi()", 1000);

}

function save_outline() {

if(document.getElementById('content').value != '') {

editor.sync();

document.cookie = 'outline_title==' + document.getElementById('title').value;

document.cookie = 'outline_author==' + document.getElementById('author').value;

document.cookie = 'outline_category_id==' + document.getElementById('category_id').value;

document.cookie = 'outline_subject_id==' + document.getElementById('subject_id').value;

document.cookie = 'outline_description==' + document.getElementById('description').value;

document.cookie = 'outline_content==' + document.getElementById('content').value.replace(/[\n\r\t]*/g, "");

document.getElementById('timing').innerHTML = 30;

}

}

function get_outline() {

if (confirm("此操作将覆盖当前帖子内容,确定要恢复数据吗?")) {

document.getElementById('title').value = getCookie('outline_title');

document.getElementById('author').value = getCookie('outline_author');

document.getElementById('category_id').value = getCookie('outline_category_id');

document.getElementById('subject_id').value = getCookie('outline_subject_id');

document.getElementById('description').value = getCookie('outline_description');

editor.html(getCookie('outline_content'));

document.getElementById('timing').innerHTML = 30;

}

}

//设置两个cookie

//                        document.cookie = "userId=828";

//                        document.cookie = "userName=hulk";

//获取Cookie

function getCookie(key) {

//获取cookie字符串

var strCookie = document.cookie;

//将多cookie切割为多个名/值对

var arrCookie = strCookie.split("; ");

//遍历cookie数组,处理每个cookie对

for (var i = 0; i < arrCookie.length; i++) {

var arr = arrCookie[i].split("==");

//找到名称为userId的cookie,并返回它的值

if (key == arr[0]) {

return arr[1];

}

}

return null;

}

function ajax_save_outline() {

editor.sync();

$.post("",

{

title: document.getElementById('title').value,

author: document.getElementById('author').value,

category_id: document.getElementById('category_id').value,

subject_id: document.getElementById('subject_id').value,

description: document.getElementById('description').value,

content: document.getElementById('content').value,

dilicms_csrf_token: $("input[name='dilicms_csrf_token']").val(),

},

function(data, status) {

console.log(data);

}

);

document.getElementById('timing').innerHTML = 30;

}

function ajax_get_outline() {

document.cookie = "dilicms_csrf_token=828";

alert(getCookie('dilicms_csrf_token'));

if (confirm("此操作将覆盖当前帖子内容,确定要恢复数据吗?")) {

$.get("", "",

function(data, status) {

if (data != '') {

var obj = JSON.parse(data);

document.getElementById('title').value = obj.title;

document.getElementById('author').value = obj.author;

document.getElementById('category_id').value = obj.category_id;

document.getElementById('subject_id').value = obj.subject_id;

document.getElementById('description').value = obj.description;

//                                            document.getElementById('content').value = obj.content;

editor.html(obj.content);

}

}

);

}

document.getElementById('timing').innerHTML = 30;

}

js存cookie时要去空格 切记

自写的  分享给大家

你可能感兴趣的:(js定时保存草稿(js设置cookie和ajax通过后台设置cookie)(包含获取kindeditor中的textarea的值))