js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能示例
來(lái)源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 21:55:15
js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能示例
js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能示例:本文實(shí)例講述了js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能。分享給大家供大家參考,具體如下: <!DOCTYPE html> <html> <head> <meta charset=utf-8> <meta http-equiv=X-UA-Compatible co
導(dǎo)讀js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能示例:本文實(shí)例講述了js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能。分享給大家供大家參考,具體如下: <!DOCTYPE html> <html> <head> <meta charset=utf-8> <meta http-equiv=X-UA-Compatible co

本文實(shí)例講述了js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能。分享給大家供大家參考,具體如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>www.gxlcms.com cookie記住用戶(hù)名</title>
<meta name="description" content="">
<meta name="keywords" content="">
</head>
<body>
<script>
//1、cookie的使用:document.cookie = 'key=value';可以同時(shí)設(shè)置多個(gè)
// document.cookie="username=longzhoufeng"
// document.cookie="age=03"
//2、cookie的過(guò)期時(shí)間:document.cookie = '名稱(chēng)=值;expires=' + 字符串格式的時(shí)間;
// var myDate=new Date()
// myDate.setDate(myDate.getDate()+3)
// document.cookie="mynameis="+encodeURI("longzhoufeng")+ ";expires="+myDate.toGMTString();
// document.cookie="age=30"
// console.log(decodeURI(document.cookie))
// 解碼后
輸出結(jié)果如下:
//mynameis=longzhoufeng; age=30
//3、把上面的封裝成一個(gè)函數(shù),其中有三個(gè)參數(shù)是在變化的,key值,value值,T時(shí)間
function setCookie(key,value,t){
var myDate=new Date()
myDate.setDate(myDate.getDate()+t)
document.cookie=key+"="+value+ ";expires="+myDate.toGMTString();
}
function getCookie(key){
var arr1 = document.cookie.split('; ');
for (var i=0; i<arr1.length; i++) {
var arr2 = arr1[i].split('=');
if ( arr2[0] == key ) {
return decodeURI(arr2[1]);
}
}
}
function removeCookie(key) {
setCookie(key, '', -1);
}
// setCookie("myName","longzhoufeng",1)
// console.log(getCookie("myName"))
// console.log(removeCookie("myName"))
// alert(typeof myDate)
// 必須將時(shí)間格式轉(zhuǎn)換成字符形式
// alert(typeof myDate.toGMTString());
//4、內(nèi)容最好編碼存放,encodeURI
//alert( encodeURI('你好') );
//alert( decodeURI('%E4%BD%A0%E5%A5%BD') )
</script>
<script>
window.onload = function() {
var oUsername = document.getElementById('username');
var oLogin = document.getElementById('login');
var oDel = document.getElementById('del');
if ( getCookie('username') ) {
oUsername.value = getCookie('username');
}
oLogin.onclick = function() {
alert('登陸成功');
setCookie('username', oUsername.value, 5);
}
oDel.onclick = function() {
removeCookie('username');
oUsername.value = '';
}
}
</script>
<input type="text" id="username" />
<input type="button" value="登陸" id="login" />
<input type="button" value="刪除" id="del" />
</body>
</html>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能示例
js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能示例:本文實(shí)例講述了js使用cookie實(shí)現(xiàn)記住用戶(hù)名功能。分享給大家供大家參考,具體如下: <!DOCTYPE html> <html> <head> <meta charset=utf-8> <meta http-equiv=X-UA-Compatible co