/ Published in: JavaScript
Expand |
Embed | Plain Text
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'; } // Usage: // Check if cookies are allowed in this browser. if cookiesAllowed() { alert('you can set cookies'); } else { alert("This browser doesn't allow cookies."); } // Create a cookie named myCookie with a value of 'Peanut Butter' // The cookie will expire in 10 days. setCookie('myCookie','Peanut Butter', 10); // Retreive the value of 'myCookie' var userCookie = getCookie('myCookie'); // Delete 'myCookie' deleteCookie('myCookie');
You need to login to post a comment.
