這篇文章主要介紹“javascript清除cookie的方法”,在日常操作中,相信很多人在javascript清除cookie的方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”javascript清除cookie的方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)主營(yíng)遂寧網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,手機(jī)APP定制開(kāi)發(fā),遂寧h5小程序設(shè)計(jì)搭建,遂寧網(wǎng)站營(yíng)銷(xiāo)推廣歡迎遂寧等地區(qū)企業(yè)咨詢(xún)
清除方法:不指定cookie值,把expires參數(shù)設(shè)置為過(guò)去的日期即可,語(yǔ)法“document.cookie="username=;expires=Thu,01 Jan 1970 00:00:00 UTC;path=/;";”。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。
Cookie 為 Web 應(yīng)用程序保存用戶(hù)相關(guān)信息提供了一種有用的方法。例如,當(dāng)用戶(hù)訪問(wèn)咱們的站點(diǎn)時(shí),可以利用 Cookie 保存用戶(hù)首選項(xiàng)或其他信息,這樣,當(dāng)用戶(hù)下次再訪問(wèn)咱們的站點(diǎn)時(shí),應(yīng)用程序就可以檢索以前保存的信息。
Cookie 是什么鬼
Cookie 是一小段文本信息,伴隨著用戶(hù)請(qǐng)求和頁(yè)面在 Web 服務(wù)器和瀏覽器之間傳遞。用戶(hù)每次訪問(wèn)站點(diǎn)時(shí),Web 應(yīng)用程序都可以讀取 Cookie 包含的信息。
Cookie的出現(xiàn)是為了解決保存用戶(hù)信息的問(wèn)題。例如
當(dāng)用戶(hù)訪問(wèn)網(wǎng)頁(yè)時(shí),用戶(hù)的名字可以存儲(chǔ)在cookie中。
下次用戶(hù)訪問(wèn)頁(yè)面時(shí),cookie會(huì)記住用戶(hù)名。
Cookie 能在所有網(wǎng)頁(yè)中記住用戶(hù)的信息。它以字符串的形式包含信息,并鍵值對(duì)的形式保存的,即key=value的格式。各個(gè)cookie之間一般是以“;”分隔。
username = Daisy Green
Cookie 的組成
Cookie 在HTTP的頭部Header信息中,HTTP Set-Cookie的Header格式如下:
Set-Cookie: name=value; [expires=date]; [path=path]; [domain=domainname]; [secure];
在HTTP代碼中一個(gè)具體的例子:
<meta http-equiv="set-cookie" content=" cookieName = cookieValue;expires=01-Dec-2006 01:14:26 GMT; path=/" />
從上面的格式可以看出,Cookie由下面幾部分組成。
Name/Value對(duì)
Name/Value由分號(hào)分隔,一個(gè)Cookie最多有20對(duì),每個(gè)網(wǎng)頁(yè)最多有一個(gè)Cookie,Value的長(zhǎng)度不超過(guò)4K。對(duì)于Value值,最好用encodeURIComponent對(duì)其編碼。
JS Cookie
在JS中,可以使用Document對(duì)象的cookie屬性操作cookie。 JS 可以讀取,創(chuàng)建,修改和刪除當(dāng)前網(wǎng)頁(yè)的cookie,,來(lái)看看具體的騷操作。
創(chuàng)建 Cookie
JS可以使用document.cookie屬性創(chuàng)建cookie,可以通過(guò)以下方式創(chuàng)建cookie:
document.cookie = "username=Daisy Green";
還可以添加有效日期(UTC 時(shí)間)。默認(rèn)情況下,在瀏覽器關(guān)閉時(shí)會(huì)刪除 cookie:
document.cookie = "username=Daisy Green; expires=Mon, 26 Aug 2019 12:00:00 UTC";
通過(guò) path 參數(shù),可以告訴瀏覽器 cookie 屬于什么路徑。默認(rèn)情況下,cookie 屬于當(dāng)前頁(yè)。
document.cookie = "username=Daisy Green; expires=Mon, 26 Aug 2019 12:00:00 UTC"; path=/";
讀取 Cookie
通過(guò) JS,可以這樣讀取 cookie:
var x = document.cookie;
document.cookie 會(huì)在一條字符串中返回所有 cookie,比如:cookie1=value; cookie2
事例:
<html> <head> <script type = "text/javascript"> <!-- function ReadCookie() { var allcookies = document.cookie; document.write ("All Cookies : " + allcookies ); // Get all the cookies pairs in an array cookiearray = allcookies.split(';'); // Now take key value pair out of this array for(var i=0; i<cookiearray.length; i++) { name = cookiearray[i].split('=')[0]; value = cookiearray[i].split('=')[1]; document.write ("Key is : " + name + " and Value is : " + value); } } //--> </script> </head> <body> <form name = "myform" action = ""> <p> click the Button to View Result:</p> <input type = "button" value = "Get Cookie" onclick = "ReadCookie()"/> </form> </body> </html>
運(yùn)行:
改變 cookie
通過(guò)使用 JS,咱們可以像創(chuàng)建 cookie 一樣改變它:
document.cookie = "username=Steve Jobs; expires=Sun, 31 Dec 2017 12:00:00 UTC; path=/";
這樣舊 cookie 會(huì)被覆蓋。
事例:
<html> <head> <script type = "text/javascript"> <!-- function WriteCookie() { var now = new Date(); now.setMonth( now.getMonth() + 1 ); cookievalue = escape(document.myform.customer.value) + ";" document.cookie = "name=" + cookievalue; document.cookie = "expires=" + now.toUTCString() + ";" document.write ("Setting Cookies : " + "name=" + cookievalue ); } //--> </script> </head> <body> <form name = "myform" action = ""> Enter name: <input type = "text" name = "customer"/> <input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/> </form> </body> </html>
運(yùn)行:
刪除 cookie
刪除 cookie 非常簡(jiǎn)單,不必指定 cookie 值:直接把 expires 參數(shù)設(shè)置為過(guò)去的日期即可:
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
應(yīng)該定義 cookie 路徑以確保刪除正確的 cookie。如果不指定路徑,有些瀏覽器不會(huì)讓咱們刪除 cookie。
事例:
<html> <head> <script type = "text/javascript"> <!-- function WriteCookie() { var now = new Date(); now.setMonth( now.getMonth() - 1 ); cookievalue = escape(document.myform.customer.value) + ";" document.cookie = "name=" + cookievalue; document.cookie = "expires=" + now.toUTCString() + ";" document.write("Setting Cookies : " + "name=" + cookievalue ); } //--> </script> </head> <body> <form name = "myform" action = ""> Enter name: <input type = "text" name = "customer"/> <input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/> </form> </body> </html>
到此,關(guān)于“javascript清除cookie的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
當(dāng)前名稱(chēng):javascript清除cookie的方法
網(wǎng)站路徑:http://muchs.cn/article42/pjjdhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、微信公眾號(hào)、動(dòng)態(tài)網(wǎng)站、小程序開(kāi)發(fā)、網(wǎng)站策劃、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)