How HTML gets and writes cookies

Source: Internet
Author: User
Tags setcookie

//取Cookie的值           function getCookie(cookie_name) {      var allcookies = document.cookie;      var cookie_pos = allcookies.indexOf(cookie_name);    //索引的长度        // 如果找到了索引,就代表cookie存在,      // 反之,就说明不存在。      if (cookie_pos != -1)      {          // 把cookie_pos放在值的开始,只要给值加1即可。          cookie_pos += cookie_name.length + 1;       //这里容易出问题,所以请大家参考的时候自己好好研究一下          var cookie_end = allcookies.indexOf( ";" , cookie_pos);            if (cookie_end == -1)          {              cookie_end = allcookies.length;          }            var value = unescape(allcookies.substring(cookie_pos, cookie_end));          //这里就可以得到你想要的cookie的值了。。。      }      return value; }      function getCookieVal(offset) {          var endstr = document.cookie.indexOf(";", offset);          if (endstr == -1) endstr = document.cookie.length;          return unescape(document.cookie.substring(offset, endstr));      }      //写入到Cookie            function SetCookie(name, value, expires) {          var argv = SetCookie.arguments;          //本例中length = 3          var argc = SetCookie.arguments.length;          var expires = (argc > 2) ? argv[2] : null;          var path = (argc > 3) ? argv[3] : null;          var domain = (argc > 4) ? argv[4] : null;          var secure = (argc > 5) ? argv[5] : false;          document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");      }

How HTML gets and writes cookies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.