Advertisement
51L3N7

Cookies

Dec 27th, 2011
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2. @author: 0x51L3N7
  3. @date: 19/12/2011
  4. @note: Doesn't work on local pages with Google Chrome and Maxthon due the limitations of the same.
  5. */
  6.  
  7. function Cookie() {
  8.         var expire = new Date();
  9.                
  10.         /*! Create and modify cookies
  11.         * @param: {string} Cookie name
  12.         * @param: {string|number|boolean} Cookie value
  13.         * @param: {number} Expiration date of cookie (In milliseconds).
  14.         */
  15.         this.set = function( name, value, expirationDate ) {
  16.                 if ( !( name && value ) ) return;
  17.                 expire.setTime( expire.getTime() + expirationDate );
  18.                 document.cookie = name + '=' + value + ( !expirationDate ? '' : ';expires=' + expire.toUTCString() );
  19.         }
  20.                
  21.         /*! Gets cookie value
  22.         * @param: {string} Cookie name
  23.         * Returns false if the cookie doesn't exist.
  24.         */
  25.         this.get = function( name ) {
  26.                 for ( i in j = document.cookie.split( ';' ) )
  27.                         if ( j[i].indexOf( name + '=' ) > -1 )
  28.                                 return j[i].substr( j[i].indexOf( '=' ) + 1, this.length );                                            
  29.                 return false;
  30.         }
  31.                
  32.         /*! Deletes the cookie
  33.         * @param: {string} Cookie name
  34.         */
  35.         this.exclude = function( name ) {
  36.                 this.set( name, this.get( name ), 1 );
  37.         }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement