daily pastebin goal
57%
SHARE
TWEET

cookiestorage.js -- rvklein.me -- [older stuff]

a guest Jan 23rd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!( rvklein.me -- cookiestorage.js -- [ Older stuff, could improve in various ways. ] )!*/
  2. /* Create an interface to cookies similar to the LocalStorage API. */
  3.  
  4. var cookieStorage = new function(){
  5.  
  6.     function getItem( n ){
  7.  
  8.         var c = document.cookie.split( '; ' ) ;
  9.         var tmp ;
  10.  
  11.         for( var j = c.length , i = 0 ; i < j ; i++ ){
  12.             tmp = c[ i ].split( '=' ) ;
  13.             if( tmp[ 1 ] ){
  14.                 return( tmp[ 1 ] );
  15.             }else{
  16.                 return( null );
  17.             }
  18.         }
  19.  
  20.     }
  21.  
  22.     function setItem( n , v , flags ){
  23.  
  24.         if( !n || !v ){ return( false ); }
  25.  
  26.         if( typeof( v ) !== 'string' ){
  27.             if( typeof( v ) === 'number' ){
  28.                 if( v == 0 || !( v % 1 ) ){
  29.                     v = parseInt( v , 10 ) ;
  30.                 }else{
  31.                     v = parseFloat( v , 10 ) ;
  32.                 }
  33.             }else{
  34.                 v = JSON.stringify( v ) ;
  35.             }
  36.         }
  37.  
  38.         var out = ( n + '=' + encodeURIComponent( v ) ) ;
  39.  
  40.         if( flags ){
  41.             if( flags.expires ){
  42.                 if( flags.expires.constructor.name === 'Date' ){
  43.                     flags.expires = flags.expires.toGMTString() ;
  44.                 }
  45.  
  46.                 out += ( '; Expires=' + flags.expires ) ;
  47.             }
  48.             if( flags.secure ){ out += ( '; Secure' ) ; }
  49.             if( flags.domain ){ out += ( '; Domain=' + flags.domain ) ; }
  50.             if( flags.path ){ out += ( '; Path=' + flags.path ) ; }
  51.         }
  52.  
  53.         document.cookie = out ;
  54.     }
  55.  
  56.     function removeItem( n ){
  57.         document.cookie = ( n + '=' + '; Expires='
  58.         +   'Thu, 01 Jan 1970 00:00:00 GMT'
  59.         ) ;
  60.     }
  61.  
  62.     this.getItem = getItem ;
  63.     this.setItem = setItem ;
  64.     this.removeItem = removeItem ;
  65. } ;
  66.  
  67. /*!(
  68.  
  69. The most notable ways to improve this include:
  70.  
  71. * setting a reasonable lower quota limit or probing for upper cookie storage limits and throwing an error when trying to assign above quota, like the LocalStorage/SessionStorage APIs
  72.  
  73. * checking to see if window.navigator.cookieEnabled equals true before writing, throw an error when trying to use cookieStorage if cookieEnabled is false. We treat it as though it were a disabled permission, as it is.
  74.  
  75. * I would probably not format my code like this any more.
  76.  
  77. )!*/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top