JS設(shè)置cookie、讀取cookie
JavaScript是運(yùn)行在客戶端的腳本,因此一般是不能夠設(shè)置Session的,因?yàn)镾ession是運(yùn)行在服務(wù)器端的。
而cookie是運(yùn)行在客戶端的,所以可以用JS來設(shè)置cookie。
js設(shè)置cookie方法匯總:
第一種:
<script>
//設(shè)置cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
//獲取cookie
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}
//清除cookie
function clearCookie(name) {
setCookie(name, "", -1);
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
checkCookie();
</script>
第二種:
<script>
//JS操作cookies方法!
//寫cookies
function setCookie(c_name, value, expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie=c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
//讀取cookies
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return (arr[2]);
else
return null;
}
//刪除cookies
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null)
document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
//使用示例
setCookie('username','Darren',30)
alert(getCookie("username"));
</script>
第三個(gè)例子
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script language="JavaScript" type="text/javascript">
function addCookie(objName, objValue, objHours){//添加cookie
var str = objName + "=" + escape(objValue);
if (objHours > 0) {//為0時(shí)不設(shè)定過期時(shí)間,瀏覽器關(guān)閉時(shí)cookie自動(dòng)消失
var date = new Date();
var ms = objHours * 3600 * 1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
alert("添加cookie成功");
}
function getCookie(objName){//獲取指定名稱的cookie的值
var arrStr = document.cookie.split("; ");
for (var i = 0; i < arrStr.length; i++) {
var temp = arrStr[i].split("=");
if (temp[0] == objName)
return unescape(temp[1]);
}
}
function delCookie(name){//為了刪除指定名稱的cookie,可以將其過期時(shí)間設(shè)定為一個(gè)過去的時(shí)間
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
}
function allCookie(){//讀取所有保存的cookie字符串
var str = document.cookie;
if (str == "") {
str = "沒有保存任何cookie";
}
alert(str);
}
function $(m, n){
return document.forms[m].elements[n].value;
}
function add_(){
var cookie_name = $("myform", "cookie_name");
var cookie_value = $("myform", "cookie_value");
var cookie_expireHours = $("myform", "cookie_expiresHours");
addCookie(cookie_name, cookie_value, cookie_expireHours);
}
function get_(){
var cookie_name = $("myform", "cookie_name");
var cookie_value = getCookie(cookie_name);
alert(cookie_value);
}
function del_(){
var cookie_name = $("myform", "cookie_name");
delCookie(cookie_name);
alert("刪除成功");
}
</script>
</head>
<body>
<form name="myform">
<div>
<label for="cookie_name">
名稱
</label>
<input type="text" name="cookie_name" />
</div>
<div>
<label for="cookie_value">
值
</lable>
<input type="text" name="cookie_value" />
</div>
<div>
<label for="cookie_expireHours">
多少個(gè)小時(shí)過期
</lable>
<input type="text" name="cookie_expiresHours" />
</div>
<div>
<input type="button" value="添加該cookie" onclick="add_()"/><input type="button" value="讀取所有cookie" onclick="allCookie()"/><input type="button" value="讀取該名稱cookie" onclick="get_()"/><input type="button" value="刪除該名稱cookie" onclick="del_()"/>
</div>
</form>
</body>
</html>
注意:
chrome瀏覽器在本地獲取不到cookie。必須在服務(wù)器上才可以。如果是本地的話,你可以放到local的www目錄下面。
Google Chrome只支持在線網(wǎng)站的cookie的讀寫操作,對(duì)本地html的cookie操作是禁止的。所以下面的代碼如果你寫在一個(gè)本地的html文件中,將彈出的對(duì)話框內(nèi)容為空。
document.cookie = "Test=cooo"; alert(document.cookie);
如果這個(gè)頁面是在線網(wǎng)站的內(nèi)容,則會(huì)正常顯示cookie內(nèi)容Test=cooo等等。
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
JavaScript 解決ajax中parsererror錯(cuò)誤案例詳解
這篇文章主要介紹了JavaScript 解決ajax中parsererror錯(cuò)誤案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
javascript驗(yàn)證只能輸入數(shù)字和一個(gè)小數(shù)點(diǎn)示例
使用javascript限制只能輸入數(shù)字和一個(gè)小數(shù)點(diǎn),在某些情況下還是比較使用的,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-10-10
如何通過非數(shù)字與字符的方式實(shí)現(xiàn)PHP WebShell詳解
這篇文章主要給大家介紹了關(guān)于如何通過非數(shù)字與字符的方式實(shí)現(xiàn)PHP WebShell的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07
js實(shí)現(xiàn)select組件的選擇輸入過濾代碼
如何實(shí)現(xiàn)select組件的選擇輸入過濾作用,下面有一段js代碼,很實(shí)用,需要的朋友可以看看2014-10-10
JavaScript實(shí)現(xiàn)獲取用戶單擊body中所有A標(biāo)簽內(nèi)容的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)獲取用戶單擊body中所有A標(biāo)簽內(nèi)容的方法,涉及javascript針對(duì)頁面元素及事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
JavaScript 字符串常用操作小結(jié)(非常實(shí)用)
這篇文章主要介紹了JavaScript 字符串常用操作的知識(shí),包括字符串截取,查找類的方法,對(duì)js字符串操作相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-11-11
文本框只能輸入數(shù)字的js代碼(含小數(shù)點(diǎn))
下面小編就為大家?guī)硪黄谋究蛑荒茌斎霐?shù)字的js代碼(含小數(shù)點(diǎn))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
Javascript類型系統(tǒng)之undefined和null淺析
這篇文章主要介紹了Javascript類型系統(tǒng)之undefined和null的知識(shí),通過本文還簡(jiǎn)單給大家介紹了javascript中null和undefined的區(qū)別的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

