javascript定時(shí)保存表單數(shù)據(jù)的代碼
更新時(shí)間:2011年03月17日 00:18:22 作者:
我相信有不少TX用過(guò)QQ或163的郵箱吧?他們中有一個(gè)比較有用且有趣的功能,如果您在編寫郵件,那在固定一個(gè)時(shí)間頻率內(nèi),它會(huì)自動(dòng)將您的郵件內(nèi)容保存起來(lái),以免丟失。
(忘記是不是兩家郵箱都有這個(gè)功能)。
那這個(gè)功能是怎么做的呢?
定時(shí),我們知道怎么弄,但保存呢?也許我們會(huì)通過(guò)隱藏域等手段來(lái)存放數(shù)據(jù)。但是,這個(gè)卻有個(gè)缺點(diǎn):那就是刷新頁(yè)面后,數(shù)據(jù)將會(huì)丟失。
而此時(shí),就該輪到我們很少關(guān)注,而且估計(jì)有不少人不知道的UserData 行為(userData Behavior)登場(chǎng)了:
而這個(gè)UserData是什么?怎么用?,我將在文章最后轉(zhuǎn)載一篇介紹它的文章。
現(xiàn)在,我直接上例子,所謂無(wú)代碼,無(wú)真相嘛:
<!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');
//自動(dòng)保存
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ù)制下來(lái)運(yùn)行一下,你就會(huì)發(fā)現(xiàn),其實(shí)這跟郵箱中的定時(shí)保存基本一致了,在潤(rùn)色一下就OK了。
大家看下利用userData實(shí)現(xiàn)客戶端保存表單數(shù)據(jù) 這篇文章。
那這個(gè)功能是怎么做的呢?
定時(shí),我們知道怎么弄,但保存呢?也許我們會(huì)通過(guò)隱藏域等手段來(lái)存放數(shù)據(jù)。但是,這個(gè)卻有個(gè)缺點(diǎn):那就是刷新頁(yè)面后,數(shù)據(jù)將會(huì)丟失。
而此時(shí),就該輪到我們很少關(guān)注,而且估計(jì)有不少人不知道的UserData 行為(userData Behavior)登場(chǎng)了:
而這個(gè)UserData是什么?怎么用?,我將在文章最后轉(zhuǎn)載一篇介紹它的文章。
現(xiàn)在,我直接上例子,所謂無(wú)代碼,無(wú)真相嘛:
復(fù)制代碼 代碼如下:
<!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');
//自動(dòng)保存
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ù)制下來(lái)運(yùn)行一下,你就會(huì)發(fā)現(xiàn),其實(shí)這跟郵箱中的定時(shí)保存基本一致了,在潤(rùn)色一下就OK了。
大家看下利用userData實(shí)現(xiàn)客戶端保存表單數(shù)據(jù) 這篇文章。
您可能感興趣的文章:
- js檢測(cè)離開(kāi)或刷新頁(yè)面時(shí)表單數(shù)據(jù)是否更改的方法
- jquery ajax 如何向jsp提交表單數(shù)據(jù)
- 深入分析JSON編碼格式提交表單數(shù)據(jù)
- 一個(gè)檢測(cè)表單數(shù)據(jù)的JavaScript實(shí)例
- JSP 重置按鈕清空傳入的表單數(shù)據(jù)實(shí)例
- js清空表單數(shù)據(jù)的兩種方式(遍歷+reset)
- javascript檢查表單數(shù)據(jù)是否改變的方法
- js下利用userData實(shí)現(xiàn)客戶端保存表單數(shù)據(jù)
- 利用JS輕松實(shí)現(xiàn)獲取表單數(shù)據(jù)
相關(guān)文章
技術(shù)男用來(lái)對(duì)妹子表白的百度首頁(yè)
這篇文章主要介紹了技術(shù)男用來(lái)對(duì)妹子表白的百度首頁(yè),非常有創(chuàng)意、有意思,需要的朋友可以參考下2014-07-07JavaScript組合模式Composite Pattern
這篇文章主要介紹了學(xué)習(xí)理解JavaScript組合模式,組合模式及Composite Pattern又叫部分整體模式,是用于把一組相似的對(duì)象當(dāng)作一個(gè)單一的對(duì)象2022-04-04JavaScript實(shí)現(xiàn)級(jí)聯(lián)菜單的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)級(jí)聯(lián)菜單的方法,涉及javascript頁(yè)面元素操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06微信小程序?qū)崿F(xiàn)手勢(shì)滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)手勢(shì)滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08使用Bootstrap Tabs選項(xiàng)卡Ajax加載數(shù)據(jù)實(shí)現(xiàn)
這篇文章主要介紹了使用Bootstrap Tabs選項(xiàng)卡Ajax加載數(shù)據(jù)實(shí)現(xiàn),以及遇到的問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Bootstrap基本組件學(xué)習(xí)筆記之按鈕組(8)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本組件學(xué)習(xí)筆記之按鈕組,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12javascript實(shí)現(xiàn)畫(huà)板功能
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)畫(huà)板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04Rxjs?中處理錯(cuò)誤和抓取錯(cuò)誤的代碼案例
這篇文章主要介紹了Rxjs?中怎么處理和抓取錯(cuò)誤,本文,我們學(xué)習(xí)了如何使用?catchError?在數(shù)據(jù)流中抓取錯(cuò)誤,怎么去修改和返回?observable,或者使用?EMPTY?不去觸發(fā)組件中的錯(cuò)誤,需要的朋友可以參考下2022-08-08