亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2014年05月27日 17:13:48   作者:  
cookie 是存儲(chǔ)于訪問(wèn)者的計(jì)算機(jī)中的變量,在這個(gè)例子中我們要?jiǎng)?chuàng)建一個(gè)存儲(chǔ)訪問(wèn)者名字的 cookie,需要的朋友可以參考下
什么是cookie?

cookie 是存儲(chǔ)于訪問(wèn)者的計(jì)算機(jī)中的變量。每當(dāng)同一臺(tái)計(jì)算機(jī)通過(guò)瀏覽器請(qǐng)求某個(gè)頁(yè)面時(shí),就會(huì)發(fā)送這個(gè) cookie。你可以使用 JavaScript 來(lái)創(chuàng)建和取回 cookie 的值。
有關(guān)cookie的例子:

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

創(chuàng)建和存儲(chǔ) cookie

在這個(gè)例子中我們要?jiǎng)?chuàng)建一個(gè)存儲(chǔ)訪問(wèn)者名字的 cookie。當(dāng)訪問(wèn)者首次訪問(wèn)網(wǎng)站時(shí),他們會(huì)被要求填寫姓名。名字會(huì)存儲(chǔ)于 cookie 中。當(dāng)訪問(wèn)者再次訪問(wèn)網(wǎng)站時(shí),他們就會(huì)收到歡迎詞。

首先,我們會(huì)創(chuàng)建一個(gè)可在 cookie 變量中存儲(chǔ)訪問(wèn)者姓名的函數(shù):
復(fù)制代碼 代碼如下:

<span style="font-size:14px;">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>

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

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

之后,我們要?jiǎng)?chuàng)建另一個(gè)函數(shù)來(lái)檢查是否已設(shè)置 cookie:
復(fù)制代碼 代碼如下:

<span style="font-size:14px;">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ù)首先會(huì)檢查 document.cookie 對(duì)象中是否存有 cookie。假如 document.cookie 對(duì)象存有某些 cookie,那么會(huì)繼續(xù)檢查我們指定的 cookie 是否已儲(chǔ)存。如果找到了我們要的 cookie,就返回值,否則返回空字符串。

最后,我們要?jiǎng)?chuàng)建一個(gè)函數(shù),這個(gè)函數(shù)的作用是:如果 cookie 已設(shè)置,則顯示歡迎詞,否則顯示提示框來(lái)要求用戶輸入名字。
復(fù)制代碼 代碼如下:

<span style="font-size:14px;">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>

這是所有的代碼:
復(fù)制代碼 代碼如下:

<span style="font-size:14px;"><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 onLoad="checkCookie()">
</body>
</html></span>

相關(guān)文章

最新評(píng)論