本文目录一览:
- 1、如何用js删除php生成的cookie
- 2、js如何删除cookie
- 3、在js中如何删除一个cookie
- 4、js删除cookie
- 5、JS 中怎么删除cookie
- 6、如何使用js删除cookies?
如何用js删除php生成的cookie
var cookiecheck = '?php echo $_COOKIE['cookiename'];?';
先说明下这个吧
该php语句的作用在于获得cookie名为'cookiename'的cookie值,假设为[cookiename],然后输出该值。
由于在php是服务器解析,所以它会在js被执行之前生成,最后你的静态页面上显示出来的就是:
var cookiecheck=[cookiename];
再 你的代码我看过了 是有效的 我注意到你的“感叹号”不是英文格式,是打错还是?
测试代码如下:
?php
SetCookie("name","this is my cookie");
?
script type="text/javascript"
function getCookie(name)
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]);
return null;
}
// 判断是否已经登录
if(!getCookie("name"))
{
alert('失败');
}else{
alert(getCookie("name"));
}
/script
js如何删除cookie
设置cookie的时间为过期时间 即可
function delCookie(name){//为cookie name
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
}
在js中如何删除一个cookie
1、简单的删除cookie
//删除cookie
function deleteCookie(name){
var date=new Date();
date.setTime(date.getTime()-10000);
document.cookie=name+"=v; expires="+date.toGMTString();
}
2、设置访问路径的cookie删除
/**
* js 删除 http cookie
* @param name 名字
* @param path 路径
* @param domain 域名
*/
function deleteCookie(name,path,domain){
if(getCookie(name)) document.cookie= name+'='+
((path)?';path='+path:'')+
((domain)?';domain='+domain:'') +
';expires=Thu,01-Jan-1970 00:00:01 GMT';
}
/**
* js取得http cookie
* @param name cookie名字
*/
function getCookie(name) {
var start = document.cookie.indexOf( name + "=");
var len = start + name.length + 1;
if((!start)(name!=document.cookie.substring(0,name.length))){
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ';', len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
js删除cookie
//读取Cookie的函数
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;
}
//写入Cookie的函数
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("PHPSESSID", new Date().getTime(), -1);
console.log(readCookie("PHPSESSID"));
JS 中怎么删除cookie
document.cookie = "xxx=yyy";
// cookie 的删除
// 就是把一个键值对设置成过去的时间
document.cookie = "xxx=zzz;expires=" + date1;date表示日期,表示cookie多久之后自动消失
如何使用js删除cookies?
script language="JavaScript" type="text/javascript"
function addCookie(objName,objValue,objHours){//添加cookie
var str = objName + "=" + escape(objValue);
if(objHours 0){//为0时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = objHours*3600*1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
alert("添加cookie成功");
}
function getCookie(objName){//获取指定名称的cookie的值
var arrStr = document.cookie.split("; ");
for(var i = 0;i arrStr.length;i ++){
var temp = arrStr[i].split("=");
if(temp[0] == objName) return unescape(temp[1]);
}
}
function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
}
function allCookie(){//读取所有保存的cookie字符串
var str = document.cookie;
if(str == ""){
str = "没有保存任何cookie";
}
alert(str);
}
function $(m,n){
return document.forms[m].elements[n].value;
}
function add_(){
var cookie_name = $("myform","cookie_name");
var cookie_value = $("myform","cookie_value");
var cookie_expireHours = $("myform","cookie_expiresHours");
addCookie(cookie_name,cookie_value,cookie_expireHours);
}
function get_(){
var cookie_name = $("myform","cookie_name");
var cookie_value = getCookie(cookie_name);
alert(cookie_value);
}
function del_(){
var cookie_name = $("myform","cookie_name");
delCookie(cookie_name);
alert("删除成功");
}
/script