Advertisement
web-fx

cookies-1.0.0.js

Jan 14th, 2021 (edited)
256
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* cookies.js - PUBLIC DOMAIN RELEASE - VERSION 1.0.0 - https://pastebin.com/RPyacWpg */
  2. var Cookies = new function ( ) {
  3.  
  4.  
  5.  
  6.  
  7. var UseCookies = false;
  8.  
  9. try { UseCookies = G_USE_COOKIES; }
  10. catch ( error ) { UseCookies = true; }
  11.  
  12.  
  13.  
  14.  
  15. this.SetUseCookies = function ( UseCookiesIn ) { UseCookies = UseCookiesIn; }
  16.  
  17.  
  18.  
  19.  
  20. this.GetUseCookies = function ( ) { return UseCookies; }
  21.  
  22.  
  23.  
  24.  
  25. this.GetCookie = function ( NameIn, DefaultValueIn ) {
  26.  
  27.  
  28. var CookieValue = Private.GetCookieValue ( NameIn );
  29. if ( CookieValue == null || CookieValue == undefined ) { CookieValue = ''; }
  30.  
  31. var CookieDefault = DefaultValueIn;
  32. if ( CookieDefault == null || CookieDefault == undefined ) { CookieDefault = ''; }
  33.  
  34. if ( CookieValue == '' && CookieDefault != '' ) {
  35.  
  36. CookieValue = CookieDefault;
  37.  
  38. this.SetCookie ( NameIn, CookieValue, 365 );
  39.  
  40. }
  41.  
  42.  
  43.  
  44.  
  45. return CookieValue;
  46.  
  47.  
  48. }
  49.  
  50.  
  51.  
  52.  
  53. this.GetCookieAndSetDaily = function ( NameIn, DefaultValueIn ) {
  54.  
  55.  
  56. var CookieValue = Private.GetCookieValue ( NameIn );
  57. if ( CookieValue == null || CookieValue == undefined ) { CookieValue = ''; }
  58.  
  59. var CookieDefault = DefaultValueIn;
  60. if ( CookieDefault == null || CookieDefault == undefined ) { CookieDefault = ''; }
  61.  
  62. var Today = new Date ( );
  63. var CookieDateName = NameIn + ' Date';
  64. var CookieDate = this.GetCookie ( CookieDateName, Today );
  65. CookieDate = Date.parse ( CookieDate );
  66. CookieDate = new Date ( CookieDate );
  67.  
  68.  
  69. var DoSet = false;
  70.  
  71. if ( CookieValue == '' && CookieDefault != '' ) {
  72.  
  73. CookieValue = CookieDefault;
  74. DoSet = true;
  75.  
  76. }
  77.  
  78. if ( DoSet == true || Today.toDateString ( ) != CookieDate.toDateString ( ) ) {
  79.  
  80. this.SetCookie ( CookieDateName, Today, 365 );
  81. this.SetCookie ( NameIn, CookieValue, 365 );
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
  88. return CookieValue;
  89.  
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96. this.SetCookie = function ( NameIn, ValueIn, DaysExpireIn ) {
  97.  
  98.  
  99. if ( UseCookies == false ) { return; }
  100.  
  101. var ExpiryDate = new Date ( );
  102.  
  103. ExpiryDate.setTime ( ExpiryDate.getTime ( ) + ( DaysExpireIn * 24 * 60 * 60 * 1000 ) );
  104.  
  105. var Expires = 'expires=' + ExpiryDate.toUTCString ( );
  106.  
  107. document.cookie = NameIn + '=' + ValueIn + '; ' + Expires + '; path=/';
  108.  
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115. var Private = {
  116.  
  117.  
  118.  
  119.  
  120. GetCookieValue: function ( NameIn ) {
  121.  
  122.  
  123. var Output = '';
  124. var CookieValue = '';
  125. var Name = NameIn + '=';
  126. var CookieArray = document.cookie.split ( ';' );
  127.  
  128. for ( var Count = 0; Count < CookieArray.length; Count++ ) {
  129.  
  130. CookieValue = CookieArray[Count];
  131. CookieValue = CookieValue.trim ( );
  132.  
  133. if ( CookieValue.indexOf( Name ) == 0 ) {
  134.  
  135. Output = CookieValue.substring ( Name.length );
  136. break;
  137.  
  138. }
  139.  
  140. }
  141.  
  142.  
  143.  
  144.  
  145. return Output;
  146.  
  147.  
  148. }
  149.  
  150.  
  151.  
  152.  
  153. };
  154.  
  155.  
  156.  
  157.  
  158. }
Advertisement
RAW Paste Data Copied
Advertisement