如何用js来操作cookie呢?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用js来操作cookie呢?相关的知识,希望对你有一定的参考价值。
请问下面这段功能如果用js操作cookie应该怎么写?
<script>
if cookie中无标记 then
document.write("呵呵1")
标记=1
if 标记=1 then
document.write(“呵呵2”)
if 标记=2 then
document.write(“呵呵3”)
</script>
js操作COOKIE,直接给document加上cookie就可以了,但是一般如果单个的加会很麻烦所以一般会直接写好一个函数,可以直接操作cookie,这样就很方便了
setCookie这个是写入cookie,第一个是名称,第二个是cookie值,第三个是过期时间
getCookie这个是查找cookie;
removeCookie这是你需要删除的cookie;
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;i<arr.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);
参考技术A <SCRIPT language="javascript">
<!--
//首先获取name就是标记
function getcookie(Name)
var search = Name + "="
var returnvalue = "";
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;
returnvalue=unescape(document.cookie.substring(offset, end))
return returnvalue;
function okla()
if (getcookie('biaoji')=='')
document.write("呵呵1");
document.cookie="biaoji=1"
else if (getcookie('biaoji')=='1')
document.write("呵呵2");
document.cookie="biaoji=2"
else if (getcookie('biaoji')=='2')
document.write("呵呵3")
okla()
//-->
</SCRIPT>
直接粘贴到html里运行,显示呵呵1,再刷新显示呵呵2再刷新显示呵呵3,不知道是不是你想要的这个效果。本回答被提问者采纳 参考技术B <SCRIPT LANGUAGE="JavaScript">
<!--
function Cookie()
var self = this;
var trim = function(str)
return str.replace(/(^\s*)|(\s*$)/g, "");
/*-------------------
Private method init()
When create a instance,all exists cookie will add as public property.
-------------------*/
var init = function()
var allcookies = document.cookie;
if (allcookies == "") return;
var cookies = allcookies.split(';');
for(var i=0; i < cookies.length; i++) // Break each pair into an array
cookies[i] = cookies[i].split('=');
for(var i = 0; i < cookies.length; i++)
self[trim(cookies[i][0])] = decodeURIComponent(cookies[i][1]);
init();
/*--------------------
Public method save()
--------------------*/
this.save = function(daysToLive, path, domain, secure)
var dt = (new Date()).getTime() + daysToLive * 24 * 60 * 60 * 1000;
for(var prop in this)
if (typeof this[prop] == 'function')
continue;
var cookie = "";
cookie = prop + '=' + encodeURIComponent(this[prop]);
if (daysToLive || daysToLive == 0) cookie += ";expires=" + new Date(dt).toUTCString();
if (path) cookie += ";path=" + path;
if (domain) cookie += "; domain=" + domain;
if (secure) cookie += ";secure";
document.cookie = cookie;
/*--------------------
Public method remove()
--------------------*/
this.remove = function(path, domain, secure)
self.save(0, path, domain, secure);
/*-----------------------
Must save(0) first then delete this object's property
If delete first,save(0) will not save anlything.
-----------------------*/
for(var prop in this)
if (typeof this[prop] != 'function')
delete this[prop];
var cookie = new Cookie("vistordata");
if (!cookie.poped)
window.alert('呵呵1');
cookie.poped=1;
cookie.save(10);
else
if(cookie.poped=1)
window.alert('呵呵2');
cookie.poped=2
cookie.save(10);
else
window.alert('呵呵3');
//-->
</SCRIPT>
如何用HttpClient来操作Cookie-CSDN论坛
参考技术A 1.服务器认证(Server Authentication)HttpClient处理服务器认证几乎是透明的,仅需要开发人员提供登录信息(login credentials)。登录信息保存在HttpState类的实例中,可以通过 setCredentials(String realm, Credentials cred)和getCredentials(String realm)来获取或设置。
HttpClient内建的自动认证,可以通过HttpMethod类的setDoAuthentication(boolean doAuthentication)方法关闭,而且这次关闭只影响HttpMethod当前的实例。
2.代理认证(proxy authentication)
除了登录信息需单独存放以外,代理认证与服务器认证几乎一致。用 setProxyCredentials(String realm, Credentials cred)和 getProxyCredentials(String realm)设、取登录信息。
3.认证方案(authentication schemes)
是HTTP中规定最早的也是最兼容的方案,遗憾的是也是最不安全的一个方案,因为它以明码传送用户名和密码。它要求一个UsernamePasswordCredentials实例,可以指定服务器端的访问空间或采用默认的登录信息。
以上是关于如何用js来操作cookie呢?的主要内容,如果未能解决你的问题,请参考以下文章