本文目录一览:
- 1、在javascript 怎么把变量写在cookie里面啊
- 2、JS设置cookie值
- 3、如何利用js将随机获取的验证码放入cookie里面
- 4、如何通过js 把值存到session 或 cookie中
- 5、如何用js向cookie中保存数据、取数据?
在javascript 怎么把变量写在cookie里面啊
var cookieUtil={
get:function(name){
var cookieName=encodeURIComponent(name) + "=",
cookieStart=document.cookie.indexOf(cookieName),
cookieValue=null;
if(cookieStart-1){
var cookieEnd=document.cookie.indexOf(';',cookieStart);
if(cookieEnd==-1){
cookieEnd=document.cookie.length;
}
cookieValue=decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length,cookieEnd));
}
return cookieValue;
},
set:function(name,value,expires,path,domain,secure){
var cookieText=encodeURIComponent(name)+"="+encodeURIComponent(value);
if(expires instanceof Date){
cookieText +="; expires="+expires.toGMTString();
}
if(path){
cookieText +="; path="+ path;
}
if(domain){
cookieText +="; domain="+ domain;
}
if(secure){
cookieText +="; secure";
}
document.cookie= cookieText;
}
}
var cookieValue="myCookie";
window.onload=function(){
cookieUtil.set('myCookieName',cookieValue,new Date('January 1,2018'),"/");
}
javascript本身并没有直接读写Cookie的操作,上面的代码是我直接写了一个Cookie的类,可以读写Cookie。
JS设置cookie值
// 保存值
var value = document.getElementById("wenben1").value;
document.cookie = 'wenben1='+value;// 保存一个值
document.cookie = 'a=12,b=34,c=56'; // 保存多个值
// 取值
var b = document.cookie; // 结果 b = 'a=12,b=34,c=56';具体要其中的哪个值再去分割
如何利用js将随机获取的验证码放入cookie里面
如果你使用的是jquery才cookie插件的话直接$.cookie("cookies名称","验证码值");就可以了。
如果是原生JS的话document.cookie="cookies名称="+验证码值;
如何通过js 把值存到session 或 cookie中
如何通过js 把值存到session 或 cookie中
: 通过js访问controller,在controller获取到session,然后setAttribute就好了
如何用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"));