/ext-4.1.0_b3/docs/source/Cookies.html

https://bitbucket.org/srogerf/javascript · HTML · 111 lines · 105 code · 6 blank · 0 comment · 0 complexity · fa9823618dd17d269ddc8403aafd713f MD5 · raw file

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-Cookies'>/**
  19. </span> * Utility class for setting/reading values from browser cookies.
  20. * Values can be written using the {@link #set} method.
  21. * Values can be read using the {@link #get} method.
  22. * A cookie can be invalidated on the client machine using the {@link #clear} method.
  23. */
  24. Ext.define('Ext.util.Cookies', {
  25. singleton: true,
  26. <span id='Ext-util-Cookies-method-set'> /**
  27. </span> * Creates a cookie with the specified name and value. Additional settings for the cookie may be optionally specified
  28. * (for example: expiration, access restriction, SSL).
  29. * @param {String} name The name of the cookie to set.
  30. * @param {Object} value The value to set for the cookie.
  31. * @param {Object} [expires] Specify an expiration date the cookie is to persist until. Note that the specified Date
  32. * object will be converted to Greenwich Mean Time (GMT).
  33. * @param {String} [path] Setting a path on the cookie restricts access to pages that match that path. Defaults to all
  34. * pages ('/').
  35. * @param {String} [domain] Setting a domain restricts access to pages on a given domain (typically used to allow
  36. * cookie access across subdomains). For example, &quot;sencha.com&quot; will create a cookie that can be accessed from any
  37. * subdomain of sencha.com, including www.sencha.com, support.sencha.com, etc.
  38. * @param {Boolean} [secure] Specify true to indicate that the cookie should only be accessible via SSL on a page
  39. * using the HTTPS protocol. Defaults to false. Note that this will only work if the page calling this code uses the
  40. * HTTPS protocol, otherwise the cookie will be created with default options.
  41. */
  42. set : function(name, value){
  43. var argv = arguments,
  44. argc = arguments.length,
  45. expires = (argc &gt; 2) ? argv[2] : null,
  46. path = (argc &gt; 3) ? argv[3] : '/',
  47. domain = (argc &gt; 4) ? argv[4] : null,
  48. secure = (argc &gt; 5) ? argv[5] : false;
  49. document.cookie = name + &quot;=&quot; + escape(value) + ((expires === null) ? &quot;&quot; : (&quot;; expires=&quot; + expires.toGMTString())) + ((path === null) ? &quot;&quot; : (&quot;; path=&quot; + path)) + ((domain === null) ? &quot;&quot; : (&quot;; domain=&quot; + domain)) + ((secure === true) ? &quot;; secure&quot; : &quot;&quot;);
  50. },
  51. <span id='Ext-util-Cookies-method-get'> /**
  52. </span> * Retrieves cookies that are accessible by the current page. If a cookie does not exist, `get()` returns null. The
  53. * following example retrieves the cookie called &quot;valid&quot; and stores the String value in the variable validStatus.
  54. *
  55. * var validStatus = Ext.util.Cookies.get(&quot;valid&quot;);
  56. *
  57. * @param {String} name The name of the cookie to get
  58. * @return {Object} Returns the cookie value for the specified name;
  59. * null if the cookie name does not exist.
  60. */
  61. get : function(name){
  62. var arg = name + &quot;=&quot;,
  63. alen = arg.length,
  64. clen = document.cookie.length,
  65. i = 0,
  66. j = 0;
  67. while(i &lt; clen){
  68. j = i + alen;
  69. if(document.cookie.substring(i, j) == arg){
  70. return this.getCookieVal(j);
  71. }
  72. i = document.cookie.indexOf(&quot; &quot;, i) + 1;
  73. if(i === 0){
  74. break;
  75. }
  76. }
  77. return null;
  78. },
  79. <span id='Ext-util-Cookies-method-clear'> /**
  80. </span> * Removes a cookie with the provided name from the browser
  81. * if found by setting its expiration date to sometime in the past.
  82. * @param {String} name The name of the cookie to remove
  83. * @param {String} [path] The path for the cookie.
  84. * This must be included if you included a path while setting the cookie.
  85. */
  86. clear : function(name, path){
  87. if(this.get(name)){
  88. path = path || '/';
  89. document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=' + path;
  90. }
  91. },
  92. <span id='Ext-util-Cookies-method-getCookieVal'> /**
  93. </span> * @private
  94. */
  95. getCookieVal : function(offset){
  96. var endstr = document.cookie.indexOf(&quot;;&quot;, offset);
  97. if(endstr == -1){
  98. endstr = document.cookie.length;
  99. }
  100. return unescape(document.cookie.substring(offset, endstr));
  101. }
  102. });
  103. </pre>
  104. </body>
  105. </html>