Cookie dry

Look Cookie from front-end development

Cookie is a browser-side storage mechanism

Raison d'etre:

  In order to solve the "how to remember user information" invented:

    When a user visits a page, his name may be stored in a cookie

    The next time the user visits the page, cookie "remembers" his name

Features:

  Session Cookie: that is, do not set the expiration time, then just close the browser Cookie will disappear, note that closing the browser instead of closing a tag

  Persistent Cookie: that is, set an expiration period (expires), close your browser at this time, if no more than set the expiration time of the Cookie will continue to exist until it expires

  Google browser can not set up local files Cookie, IE, Firefox can

  As long as the browser is closed Cookie feature will not achieve all the Cookie feature

Set Cookie:

  document.cookie = "name = tizi"

  Which can be added on the path, not the default settings of the current page, and can not get to a higher level in Cookie page, but in the lower pages can (do a few experiments, I do not know is not absolute):

  As file: /// D: set at /MySite/test.html Cookie, in the file: may acquire the /MySite/src/test.html, it is not contrary: /// D

  However, if you set path = /, above turn, can be:

  As file: /// D: set at /MySite/test.html Cookie, in the file: may acquire the /MySite/src/test.html, and vice versa it: /// D

Get Cookie:

  console.log(document.cookie)

Change Cookie:

  This system increased nt.co Cookie = "name = we ziChange"

Delete Cookie:

  document.cookie = "name = ; expires = Thu, 01 Jan 1970 00:00:00 UTC"

 1 <html>
 2     <button onclick="setC()">设置Cookie</button>
 3     <button onclick="setCDate()">获取Cookie过期时间</button>
 4     <button onclick="getC()">获取Cookie</button>
 5     <button onclick="changeC()">更改Cookie</button>
 6     <button onclick="delC()"> Remove cookies </ Button > 
. 7      < Script > 
. 8          // set cookies 
. 9          function SetC () {
 10              the document.cookie =  " name = Tizi; " 
. 11          }
 12 is          // provided, for example a two-day of age = 20 and expired cookie 
13 is          function setCDate () {
 14              var DATE =  new new a Date ()
 15              date.setTime (date.getTime () +  2 * 24 * 60 * 60 *1000 )
 16              the document.cookie =  " Age = 20 is; Expires = "  + date.toUTCString ()
 . 17          }
 18 is          // Get Cookie 
. 19          function getc () {
 20 is              the console.log (the document.cookie)
 21 is          }
 22 is          // Change Cookie 
23 is          function changeC () {
 24              the document.cookie =  " name = tiziChange; " 
25          }
 26 is          // delete the Cookie, arranged over time expires want to 
27          function delC(){
28             document.cookie = "name = ; expires = Thu, 01 Jan 1970 00:00:00 UTC"
29         }
30     </script>
31 </html>

In fact, I refer to is W3School, for the convenience of their own memories, write a bit, quack ~

      

 

Guess you like

Origin www.cnblogs.com/tizi/p/11350888.html