cookies.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /****************************************************************
  2. ** Licensed Materials - Property of IBM
  3. **
  4. ** IBM Cognos Products: mdsrv
  5. **
  6. ** (C) Copyright IBM Corp. 2008, 2010
  7. **
  8. ** US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *****************************************************************/
  10. //***********************************************************************************************
  11. // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. //
  14. // Component: Cookie Manager
  15. //***********************************************************************************************
  16. //---------------------------------------------------------------------
  17. // class CCookieMgr
  18. //---------------------------------------------------------------------
  19. function CCookieMgr ()
  20. {
  21. this.isCookieEnabled = function () { return navigator.cookieEnabled; }
  22. this.getExpDate = function ( days, hours, minutes )
  23. {
  24. var expDate = new Date();
  25. if ( typeof days == "number" && typeof hours == "number" && typeof minutes == "number" )
  26. {
  27. expDate.setDate ( expDate.getDate() + parseInt( days ) );
  28. expDate.setHours ( expDate.getHours() + parseInt( hours ) );
  29. expDate.setMinutes ( expDate.getMinutes() + parseInt( minutes ) );
  30. return expDate.toUTCString();
  31. }
  32. }
  33. this.getCookieVal = function ( offset )
  34. {
  35. var endstr = document.cookie.indexOf ( ";", offset );
  36. if ( endstr == -1 )
  37. {
  38. endstr = document.cookie.length;
  39. }
  40. return decodeURI ( document.cookie.substring( offset, endstr ) );
  41. }
  42. this.getCookie = function ( name )
  43. {
  44. var arg = name + "=";
  45. var alen = arg.length;
  46. var clen = document.cookie.length;
  47. var i = 0;
  48. while ( i < clen )
  49. {
  50. var j = i + alen;
  51. if ( document.cookie.substring( i, j ) == arg )
  52. {
  53. return this.getCookieVal(j);
  54. }
  55. i = document.cookie.indexOf( " ", i ) + 1;
  56. if ( i == 0 )
  57. break;
  58. }
  59. return "";
  60. }
  61. this.setCookie = function ( name, value, expires, path, domain, secure )
  62. {
  63. document.cookie = name + "=" + encodeURI( value ) +
  64. ( (expires) ? "; expires=" + expires : "" ) +
  65. ( (path) ? "; path=" + path : "" ) +
  66. ( (domain) ? "; domain=" + domain : "" ) +
  67. ( (secure) ? "; secure" : "" );
  68. }
  69. this.deleteCookie = function ( name, path, domain )
  70. {
  71. if ( this.getCookie(name) )
  72. {
  73. document.cookie = name + "=" +
  74. ( (path) ? "; path=" + path : "" ) +
  75. ( (domain) ? "; domain=" + domain : "" ) +
  76. "; expires=Thu, 01-Jan-70 00:00:01 GMT"; //HACK: delete the cookie by setting expired expiration date
  77. }
  78. }
  79. }