javascript定時保存表單數(shù)據(jù)的代碼
更新時間:2011年03月17日 00:18:22 作者:
我相信有不少TX用過QQ或163的郵箱吧?他們中有一個比較有用且有趣的功能,如果您在編寫郵件,那在固定一個時間頻率內(nèi),它會自動將您的郵件內(nèi)容保存起來,以免丟失。
(忘記是不是兩家郵箱都有這個功能)。
那這個功能是怎么做的呢?
定時,我們知道怎么弄,但保存呢?也許我們會通過隱藏域等手段來存放數(shù)據(jù)。但是,這個卻有個缺點:那就是刷新頁面后,數(shù)據(jù)將會丟失。
而此時,就該輪到我們很少關注,而且估計有不少人不知道的UserData 行為(userData Behavior)登場了:
而這個UserData是什么?怎么用?,我將在文章最后轉載一篇介紹它的文章。
現(xiàn)在,我直接上例子,所謂無代碼,無真相嘛:
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript">
window.onload=function(){
var txtObj = document.getElementById('txt1');
var spanObj = document.getElementById('s1');
//自動保存
txtObj.addBehavior("#default#userData");
var saveTimer= setInterval(function(){
txtObj.setAttribute('OValue',txtObj.value);
txtObj.save('SavedData');
spanObj.innerText='數(shù)據(jù)保存于:'+(new Date());
setTimeout(function(){
spanObj.innerText='';
},1000);
},10000); //每分鐘保存一次
document.getElementById('btn1').attachEvent('onclick',function(){
clearInterval(saveTimer); //取消保存
txtObj.removeAttribute('OValue');
});
document.getElementById('btn2').attachEvent('onclick',function(){
txtObj.load('SavedData');
alert(txtObj.getAttribute('OValue'));
//txtObj.value = txtObj.getAttribute('OValue');
});
};
</script>
</head>
<body>
<span id="s1" style="color:red;"></span>
<p />
<textarea height="500" style="height:500px;width:500px;" id="txt1">
</textarea>
<p />
<input type="button" id="btn1" value="停止保存" />
<input type="button" id="btn2" value="獲取保存的值" />
</body>
</html>
將這段html復制下來運行一下,你就會發(fā)現(xiàn),其實這跟郵箱中的定時保存基本一致了,在潤色一下就OK了。
大家看下利用userData實現(xiàn)客戶端保存表單數(shù)據(jù) 這篇文章。
那這個功能是怎么做的呢?
定時,我們知道怎么弄,但保存呢?也許我們會通過隱藏域等手段來存放數(shù)據(jù)。但是,這個卻有個缺點:那就是刷新頁面后,數(shù)據(jù)將會丟失。
而此時,就該輪到我們很少關注,而且估計有不少人不知道的UserData 行為(userData Behavior)登場了:
而這個UserData是什么?怎么用?,我將在文章最后轉載一篇介紹它的文章。
現(xiàn)在,我直接上例子,所謂無代碼,無真相嘛:
復制代碼 代碼如下:
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript">
window.onload=function(){
var txtObj = document.getElementById('txt1');
var spanObj = document.getElementById('s1');
//自動保存
txtObj.addBehavior("#default#userData");
var saveTimer= setInterval(function(){
txtObj.setAttribute('OValue',txtObj.value);
txtObj.save('SavedData');
spanObj.innerText='數(shù)據(jù)保存于:'+(new Date());
setTimeout(function(){
spanObj.innerText='';
},1000);
},10000); //每分鐘保存一次
document.getElementById('btn1').attachEvent('onclick',function(){
clearInterval(saveTimer); //取消保存
txtObj.removeAttribute('OValue');
});
document.getElementById('btn2').attachEvent('onclick',function(){
txtObj.load('SavedData');
alert(txtObj.getAttribute('OValue'));
//txtObj.value = txtObj.getAttribute('OValue');
});
};
</script>
</head>
<body>
<span id="s1" style="color:red;"></span>
<p />
<textarea height="500" style="height:500px;width:500px;" id="txt1">
</textarea>
<p />
<input type="button" id="btn1" value="停止保存" />
<input type="button" id="btn2" value="獲取保存的值" />
</body>
</html>
將這段html復制下來運行一下,你就會發(fā)現(xiàn),其實這跟郵箱中的定時保存基本一致了,在潤色一下就OK了。
大家看下利用userData實現(xiàn)客戶端保存表單數(shù)據(jù) 這篇文章。
相關文章
JavaScript組合模式Composite Pattern
這篇文章主要介紹了學習理解JavaScript組合模式,組合模式及Composite Pattern又叫部分整體模式,是用于把一組相似的對象當作一個單一的對象2022-04-04JavaScript實現(xiàn)級聯(lián)菜單的方法
這篇文章主要介紹了JavaScript實現(xiàn)級聯(lián)菜單的方法,涉及javascript頁面元素操作的相關技巧,需要的朋友可以參考下2015-06-06使用Bootstrap Tabs選項卡Ajax加載數(shù)據(jù)實現(xiàn)
這篇文章主要介紹了使用Bootstrap Tabs選項卡Ajax加載數(shù)據(jù)實現(xiàn),以及遇到的問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12