javascript get/set/del cookies


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. getCookie(), setCookie(), deleteCookie() open domain
  2.  
  3. function getCookie( name ) {
  4. var start = document.cookie.indexOf( name + "=" );
  5. var len = start + name.length + 1;
  6. if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
  7. return null;
  8. }
  9. if ( start == -1 ) return null;
  10. var end = document.cookie.indexOf( ';', len );
  11. if ( end == -1 ) end = document.cookie.length;
  12. return unescape( document.cookie.substring( len, end ) );
  13. }
  14.  
  15. function setCookie( name, value, expires, path, domain, secure ) {
  16. var today = new Date();
  17. today.setTime( today.getTime() );
  18. if ( expires ) {
  19. expires = expires * 1000 * 60 * 60 * 24;
  20. }
  21. var expires_date = new Date( today.getTime() + (expires) );
  22. document.cookie = name+'='+escape( value ) +
  23. ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
  24. ( ( path ) ? ';path=' + path : '' ) +
  25. ( ( domain ) ? ';domain=' + domain : '' ) +
  26. ( ( secure ) ? ';secure' : '' );
  27. }
  28.  
  29. function deleteCookie( name, path, domain ) {
  30. if ( getCookie( name ) ) document.cookie = name + '=' +
  31. ( ( path ) ? ';path=' + path : '') +
  32. ( ( domain ) ? ';domain=' + domain : '' ) +
  33. ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
  34. }

URL: http://www.dustindiaz.com/top-ten-javascript/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.