国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

什么是cookie?js手動創(chuàng)建和存儲cookie

2024-05-06 16:06:41
字體:
來源:轉載
供稿:網(wǎng)友
cookie 是存儲于訪問者的計算機中的變量,在這個例子中我們要創(chuàng)建一個存儲訪問者名字的 cookie,需要的朋友可以參考下

什么是cookie?

cookie 是存儲于訪問者的計算機中的變量。每當同一臺計算機通過瀏覽器請求某個頁面時,就會發(fā)送這個 cookie。你可以使用 JavaScript 來創(chuàng)建和取回 cookie 的值。
有關cookie的例子:

名字 cookie
當訪問者首次訪問頁面時,他或她也許會填寫他/她們的名字。名字會存儲于 cookie 中。當訪問者再次訪問網(wǎng)站時,他們會收到類似 "Welcome John Doe!" 的歡迎詞。而名字則是從 cookie 中取回的。
密碼 cookie
當訪問者首次訪問頁面時,他或她也許會填寫他/她們的密碼。密碼也可被存儲于 cookie 中。當他們再次訪問網(wǎng)站時,密碼就會從 cookie 中取回。
日期 cookie
當訪問者首次訪問你的網(wǎng)站時,當前的日期可存儲于 cookie 中。當他們再次訪問網(wǎng)站時,他們會收到類似這樣的一條消息:"Your last visit was on Tuesday August 11, 2005!"。日期也是從 cookie 中取回的。

創(chuàng)建和存儲 cookie

在這個例子中我們要創(chuàng)建一個存儲訪問者名字的 cookie。當訪問者首次訪問網(wǎng)站時,他們會被要求填寫姓名。名字會存儲于 cookie 中。當訪問者再次訪問網(wǎng)站時,他們就會收到歡迎詞。

首先,我們會創(chuàng)建一個可在 cookie 變量中存儲訪問者姓名的函數(shù):

復制代碼 代碼如下:


<span>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())
}</span>


上面這個函數(shù)中的參數(shù)存有 cookie 的名稱、值以及過期天數(shù)。

在上面的函數(shù)中,我們首先將天數(shù)轉換為有效的日期,然后,我們將 cookie 名稱、值及其過期日期存入 document.cookie 對象。

之后,我們要創(chuàng)建另一個函數(shù)來檢查是否已設置 cookie:

復制代碼 代碼如下:


<span>function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}</span>


上面的函數(shù)首先會檢查 document.cookie 對象中是否存有 cookie。假如 document.cookie 對象存有某些 cookie,那么會繼續(xù)檢查我們指定的 cookie 是否已儲存。如果找到了我們要的 cookie,就返回值,否則返回空字符串。

最后,我們要創(chuàng)建一個函數(shù),這個函數(shù)的作用是:如果 cookie 已設置,則顯示歡迎詞,否則顯示提示框來要求用戶輸入名字。

復制代碼 代碼如下:


<span>function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
{alert('Welcome again '+username+'!')}
else
{
username=prompt('Please enter your name:',"")
if (username!=null && username!="")
{
setCookie('username',username,365)
}
}
}</span>


這是所有的代碼:

復制代碼 代碼如下:


<span><html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}

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())
}

function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
{alert('Welcome again '+username+'!')}
else
{
username=prompt('Please enter your name:',"")
if (username!=null && username!="")
{
setCookie('username',username,365)
}
}
}
</script>
</head>

<body>
</body>
</html></span>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 屏边| 二连浩特市| 广汉市| 二连浩特市| 赣榆县| 南宫市| 闻喜县| 通河县| 炉霍县| 疏勒县| 清新县| 张家港市| 木里| 广西| 石景山区| 万安县| 盈江县| 共和县| 灵山县| 鹰潭市| 和林格尔县| 巴东县| 景东| 龙井市| 彩票| 临安市| 含山县| 潍坊市| 酒泉市| 兴海县| 平和县| 晋江市| 霍州市| 扶绥县| 会同县| 广水市| 邮箱| 竹北市| 泰宁县| 孟州市| 礼泉县|