原创文章如转载,请注明:转载自 xajax学习站
本文链接 : http://xajax.dayanmei.com/20.htm

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

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

fckeditor自动保存草稿演示地址
http://xajax.dayanmei.com/demo/xajax2fckeditor/

xajax给fckeditor增加自动保存草稿下载(xajax和fckeditor请自行下载)
http://xajax.dayanmei.com/download/xajax2fckeditor.rar

<?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)) > $time){

               @unlink($dir.$filename);

               }

     }

}



function xml_escape($string) {

     return str_replace(array('&','"',"'",'<','>'),

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

          $string     

     );

}



//执行清理

clearcache($cachedir);



require_once('./xajax/xajax.inc.php');



$xajax = new xajax();

$xajax->registerFunction('autosave');

$xajax->registerFunction('loadcache');

$xajax->processRequest();

echo $xajax->getJavascript('./xajax');



//定义载入数据函数

function loadcache() {

     global $cachedir;

     $filename = $cachedir . session_id().'.txt';

     if(file_exists($filename)){

          $content = '';

          $handle = file($filename);

          foreach($handle as $key=>$val) {

               $content .= $val;

          }

          }else{

          $content = '';

          }

     $content = xml_escape($content);

     $obj = new xajaxResponse();

     $obj->call("
SetContents('$content')");

     //$obj->alert($content);

     $obj->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->assign("
autosavemsg","innerHTML","成功保存数据...");

          }

     //$obj->alert($content);

     return $obj;

}



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

     //载入编辑器

     require_once('../../includes/fckeditor/fckeditor.php');

     $editor = new fckeditor($editorid);

     $editor->BasePath = '../../includes/fckeditor/';

     $editor->Value = $content;

     return $editor->createhtml();

}



?>

<!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>

<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<
title></title>

<
meta name="Description" content="" />

</
head>

<
style type="text/css">

<!--

{

     
cursorpointer;

}

-->

</
style>

<
body>



<
form name="f1" id="f1" method="post" action="" enctype="multipart/form-data">

<?
php

//载入编辑器

echo editor('content');

?>

<script language="javascript">

<!--

// 自动保存时间间隔

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 ) ;

}

//-->

</script>

</form>

<div id="autosavemsg">

    

</div>



<a href="#" onclick="xajax_loadcache();" title="">载入自动保存内容</a> <a href="#" onclick="GetContents('content');" title="">手动保存</a>

</body>

</html>

上一篇:xajax如何获取fckeditor的值 下一篇:ajax拖动层,保存层位置

时间:2007-11-07 | 分类:xajax例子 评论:阅读:2017
标签:xajax,fckeditor,自动保存

发表评论

好多网友都好踊跃,这是好的,但请不要发表无意义的评论,谢谢

最新文章 订阅xajax站最新文章

最新评论

热门文章