本文目錄一覽:
- 1、JS 怎麼保存Cookie~~
- 2、js本地存儲和cookie
- 3、如何通過js 把值存到session 或 cookie中
- 4、前台在JavaScript方法中怎樣保存和提取Cookie
- 5、如何用js向cookie中保存數據、取數據?
- 6、原生JS如何向cookie裡面保存小量數據
JS 怎麼保存Cookie~~
js保存COOKIE,直接給document加上cookie就可以了,但是一般如果單個的加會很麻煩所以一般會直接寫好一個函數,可以直接操作cookie,這樣就很方便了
setCookie這個是寫入cookie,第一個是名稱,第二個是cookie值,第三個是過期時間
getCookie這個是查找cookie;
removeCookie這是你需要刪除的cookie;
function setCookie(name, value, iDay)
{
var oDate=new Date();
oDate.setDate(oDate.getDate()+iDay);
document.cookie=name+’=’+encodeURIComponent(value)+’;expires=’+oDate;
}
function getCookie(name)
{
var arr=document.cookie.split(‘; ‘);
var i=0;
for(i=0;iarr.length;i++)
{
//arr2-[‘username’, ‘abc’]
var arr2=arr[i].split(‘=’);
if(arr2[0]==name)
{
var getC = decodeURIComponent(arr2[1]);
return getC;
}
}
return ”;
}
function removeCookie(name)
{
setCookie(name, ‘1’, -1);
}
js本地存儲和cookie
1、數據存儲在用戶瀏覽器中
2、設置、讀取方便,甚至頁面刷新不丟失數據
3、容量較大,sessionStorage約5M, localStorage約20M
4、只存儲字元串
1、生命周期為關閉瀏覽器
2、在同一個窗口(頁面)下數據可以共享
3、以健值對的形式存儲使用
1、生命周期永久生效,除非手動刪除,否則頁面關閉也會存在
2、可以多頁面共享
3、以健值對的形式存儲使用
使用場景
如何通過js 把值存到session 或 cookie中
js存到 cookie 中沒什麼問題,只要瀏覽器開啟cookie功能就可以了。
但是存到 session 中不行,需要通過服務端語言才可以。比如java,php之類,當然如果你後台使用的是nodejs,也可以使用javascript 存入session。
1
2
3
4
var Days = 10; //此 cookie 將被保存 10 天
var exp = new Date(); //new Date(“December 31, 9998”);
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = “nam=abc;expire=”+ exp.toGMTString();
如上,就是js存到cookie中的代碼
前台在JavaScript方法中怎樣保存和提取Cookie
function readCookie()
{
var the_cookie = document.cookie;
var broken_cookie = the_cookie.split(“:”);
var the_name = broken_cookie[1];
var the_name = unescape(the_name);
alert(“Your name is: ” + the_name);
}
第1行很重要.當你的瀏覽器打開一個網頁時,它調用任何和
該網頁有關的cookie然後將其載入document.cookie屬性.
讀取cookie的技巧在於從中抽取出你需要的信息.注意在我們
所設置的cookie是這樣的:wm_javascript=username:dave%
20thau.在該函數第1行之後的所有用於從該cookie中提取出
用戶名(username).
var broken_cookie = the_cookie.split(“:”);
將cookie在分號處分割成兩部分.
var the_name = broken_cookie[1];
抓取分號後面的內容dave%20thau.
var the_name = unescape(the_name);
取消函數escape()的編碼替換.在本例中重新用空格替換了%20.
alert(“Your name is: ” + the_name); 顯示你的姓名.
這個例子使用的cookie只保存了很少的信息:用戶名,cookie
最多可以保存多達4kb的信息。
如何用js向cookie中保存數據、取數據?
用js向cookie中保存數據、獲取數據的方法如下:
function GetCookieVal(offset)
//獲得Cookie解碼後的值
{
var endstr = document.cookie.indexOf (“;”, offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
//—————————
function SetCookie(name, value)
//設定Cookie值
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc 2) ? argv[2] : null;
var path = (argc 3) ? argv[3] : null;
var domain = (argc 4) ? argv[4] : null;
var secure = (argc 5) ? argv[5] : false;
if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
document.cookie = name + “=” + escape (value) +((expires == null) ? “” : (“; expires=”+ expdate.toGMTString()))
+((path == null) ? “” : (“; path=” + path)) +((domain == null) ? “” : (“; domain=” + domain))
+((secure == true) ? “; secure” : “”);
}
//———————————
function DelCookie(name)
//刪除Cookie
{
var exp = new Date();
exp.setTime (exp.getTime() – 1);
var cval = GetCookie (name);
document.cookie = name + “=” + cval + “; expires=”+ exp.toGMTString();
}
//————————————
function GetCookie(name)
//獲得Cookie的原始值
{
var arg = name + “=”;
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return GetCookieVal (j);
i = document.cookie.indexOf(” “, i) + 1;
if (i == 0) break;
}
return null;
}
SetCookie(“username1”,99);
alert(GetCookie(“username1”));
原生JS如何向cookie裡面保存小量數據
!DOCTYPE HTML
html lang=”en-US”
head
meta charset=”UTF-8″
meta name=”keywords” content=”白菜編輯部”
title白菜編輯部/title
style type=”text/css”
/style
script type=”text/javascript”
function readCookie (name)
{
var cookieValue = “”;
var search = name + “=”;
if (document.cookie.length 0)
{
offset = document.cookie.indexOf (search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf (“;”, offset);
if (end == -1)
end = document.cookie.length;
cookieValue = unescape (document.cookie.substring (offset, end))
}
}
return cookieValue;
}
function writeCookie (name, value, hours)
{
var expire = “”;
if (hours != null)
{
expire = new Date ((new Date ()).getTime () + hours * 3600000);
expire = “; expires=” + expire.toGMTString ();
}
document.cookie = name + “=” + escape (value) + expire;
}
writeCookie (“myCookie”, “my name”, 24);
alert (readCookie (“myCookie”));
/script
/head
body
/body
/html
原創文章,作者:XOQS,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/139151.html