1、简单的删除cookie
//删除cookiefunction 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,js添加cookie,js删除cookie,2008-09-26 10:02示例如下:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=GB2312" />
<title>cookie处理函数练习(为我所写,非我所想:改善面向对象)</title>
<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 = 0i <arrStr.lengthi ++){
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 + "=aexpires=" + date.toGMTString()
}
function allCookie(){//读取所有保存的cookie字符串
var str = document.cookie
if(str == ""){
str = "没有保存任何cookie"
}
alert(str)
}
function $(m,$False$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>
</head>
<body>
<form name="myform">
<div><label for="cookie_name">名称</label><input type="text" name="cookie_name" /></div>
<div><label for="cookie_value">值</lable><input type="text" name="cookie_value" /></div>
<div><label for="cookie_expireHours">多少个小时过期</lable><input type="text" name="cookie_expiresHours" /></div>
<div>
<input type="button" value="添加该cookie" onclick="add_()" />
<input type="button" value="读取所有cookie" onclick="allCookie()" />
<input type="button" value="读取该名称cookie" onclick="get_()" />
<input type="button" value="删除该名称cookie" onclick="del_()" />
</div>
</form>
<hr />
</body>
</html>
window.close()
<script language="javascript" type="text/javascript">//关闭所有窗口就是关闭浏览器
function closeWindow()
{
var ua=navigator.userAgent
var ie=navigator.appName=="Microsoft Internet Explorer"?true:false
if(ie)
{
var IEversion=parseFloat(ua.substring(ua.indexOf("MSIE ")+5,ua.indexOf("",ua.indexOf("MSIE "))))
if(IEversion< 5.5)
{
var str = '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">'
str += '<param name="Command" value="Close"></object>'
document.body.insertAdjacentHTML("beforeEnd", str)
document.all.noTipClose.Click()
}
else
{
parent.window.opener =null
parent.window.open('','_self','')//for IE7
parent.window.close()
}
}
else
{
parent.window.close()
}
}
//清楚所有cookie
function clearCookie(){
var keys=document.cookie.match(/[^ =]+(?=\=)/g)
if (keys) {
for (var i = keys.length i--)
document.cookie=keys[i]+'=0expires=' + new Date( 0).toUTCString()
}
}
</script>
<input type="button" value="Close the Window" onclick="closeWindow()" />