---
title: "通过xajax给fckeditor增加自动保存草稿功能"
author: "txf2004"
date: "2009-05-20T10:09:00Z"
tags: []
slug: "xajax-fckeditor"
draft: false
lastmod: "2009-09-18T21:23:39Z"
id: 471211
url: "https://txf2004.iteye.com/blog/471211"
---

使用的xajax版本是0.5.4beta,fckeditor版本是2.4.3,通过xajax给fckeditor增加自动保存草稿功能,

本文侧重于演示，使用sessionid记录对应关系，用户关闭浏览器後数据就会消失，在实际应用中可以使用数据库存储数据，修改保存函数autosave和调用函数loadcache 即可.

&lt;?php

session\_start();

define('ROOT\_DIR',str\_replace("\\","/",dirname(\_\_FILE\_\_)));

$cachedir = './cache/';

//清除超过1天的记录文件

function clearcache($dir,$date = 1) {

$now = time();

$time = $date * 60 * 60 * 24;

if(!is\_dir($dir)){

die('error :dir' . $dir . ' is not exist');

}

$handle = opendir($dir);

while(false !== ($filename = readdir($handle))) {

if($filename == '.' OR $filename == '..' OR $filename == ''){

continue;

}

if(($now - filemtime($dir.$filename)) &gt; $time){

@unlink($dir.$filename);

}

}

}

function xml\_escape($string) {

return str\_replace(array('&','"',"'",'&lt;','&gt;'),

array('&amp;','&quot;','&apos;','&lt;','&gt;'),

$string

);

}

//执行清理

clearcache($cachedir);

require\_once('./xajax/xajax.inc.php');

$xajax = new xajax();

$xajax-&gt;registerFunction('autosave');

$xajax-&gt;registerFunction('loadcache');

$xajax-&gt;processRequest();

echo $xajax-&gt;getJavascript('./xajax');

//定义载入数据函数

function loadcache() {

global $cachedir;

$filename = $cachedir . session\_id().'.txt';

if(file\_exists($filename)){

$content = '';

$handle = file($filename);

foreach($handle as $key=&gt;$val) {

$content .= $val;

}

}else{

$content = '';

}

$content = xml\_escape($content);

$obj = new xajaxResponse();

$obj-&gt;call("SetContents('$content')");

//$obj-&gt;alert($content);

$obj-&gt;assign('autosavemsg','innerHTML',"成功载入数据");

return $obj;

}

//定义自动保存函数

function autosave($content) {

global $cachedir;

$obj = new xajaxResponse();

$content = mysql\_escape\_string($content);

$filename = $cachedir . session\_id() . ".txt";

$fp = fopen($filename,"w+");

fwrite($fp,$content);

fclose($fp);

if(file\_exists($filename)){

$obj-&gt;assign("autosavemsg","innerHTML","成功保存数据...");

}

//$obj-&gt;alert($content);

return $obj;

}

function editor($editorid,$content = '') {

//载入编辑器

require\_once('../../includes/fckeditor/fckeditor.php');

$editor = new fckeditor($editorid);

$editor-&gt;BasePath = '../../includes/fckeditor/';

$editor-&gt;Value = $content;

return $editor-&gt;createhtml();

}

?&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;

&lt;head&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;

&lt;title&gt;&lt;/title&gt;

&lt;meta name="Description" content="" /&gt;

&lt;/head&gt;

&lt;style type="text/css"&gt;

&lt;!--

a {

cursor: pointer;

}

--&gt;

&lt;/style&gt;

&lt;body&gt;

&lt;form name="f1" id="f1" method="post" action="" enctype="multipart/form-data"&gt;

&lt;?php

//载入编辑器

echo editor('content');

?&gt;

&lt;script language="javascript"&gt;

&lt;!--

// 自动保存时间间隔

var AutoSaveTime=90000;

// 计时器对象

var AutoSaveTimer;

// 首先设置一次自动保存状态

SetAutoSave();

// 设置自动保存状态函数

function SetAutoSave() {

AutoSaveTimer=setInterval("GetContents('content')",AutoSaveTime);

}

//获取内容

function GetContents(contentid) {

var oEditor = FCKeditorAPI.GetInstance(contentid) ;

content = oEditor.GetXHTML( true );

xajax\_autosave(content);

}

//设置编辑器内容

function SetContents(content)

{

// Get the editor instance that we want to interact with.

var oEditor = FCKeditorAPI.GetInstance('content') ;

// Set the editor contents (replace the actual one).

oEditor.SetHTML( content ) ;

}

//--&gt;

&lt;/script&gt;

&lt;/form&gt;

&lt;div id="autosavemsg"&gt;

&lt;/div&gt;

&lt;a href="#" onclick="xajax\_loadcache();" title=""&gt;载入自动保存内容&lt;/a&gt; &lt;a href="#" onclick="GetContents('content');" title=""&gt;手动保存&lt;/a&gt;

&lt;/body&gt;

&lt;/html&gt;

[http://www.corange.cn/archives/2008/10/2082.html](http://www.corange.cn/archives/2008/10/2082.html)