javascript cookie 操作函数
直接上代码:
function cookiesAllowed() {
setCookie(‘checkCookie’, ‘test’, 1);
if (getCookie(‘checkCookie’)) {
deleteCookie(‘checkCookie’);
return true;
}
return false;
}
function setCookie(name,value,expires, options) {
if (options===undefined) { options = {}; }
if ( expires ) {
var expires_date = new Date();
expires_date.setDate(expires_date.getDate() + expires)
}
document.cookie = name+’=’+escape( value ) +
( ( expires ) ? ‘;expires=’+expires_date.toGMTString() : ‘’ ) +
( ( options.path ) ? ‘;path=’ + options.path : ‘’ ) +
( ( options.domain ) ? ‘;domain=’ + options.domain : ‘’ ) +
( ( options.secure ) ? ‘;secure’ : ‘’ );
}
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 ) );
}
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’;
}