首页 > 编程知识 正文

js存储数据cookie,js存储数据

时间:2024-04-24 11:43:12 阅读:333863 作者:BJSG

本文目录一览:

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。