How to Set, retrieve, and delete cookies in javascript _ javascript skills

Source: Internet
Author: User
This article describes how to set, obtain, and delete cookies in javascript. It involves common javascript Cookie operations and is very simple and practical, for more information about how to set, retrieve, and delete cookies in javascript, see the following example. Share it with you for your reference. The specific implementation method is as follows:

/** Set Cookie ** name: Key of the cookie * value: value of the cookie * expires: Effective time of the cookie * path: specify the path * domain: Specify the host name * secure: Security */function setCookie (name, value, expires, path, domain, secure) {// cookie key value pair var str = name + "=" + escape (value); // set the cookie validity period, in the unit of hour if (expires> 0) {var date = new Date (); var MS = expires * 3600*1000; date. setTime (date. getTime () + MS); str + = "; expires =" + Date. toGMTString ();} if (path) {str + = "; path =" + path;} if (domain) {str + = "; domain =" + domain ;} if (secure) {str + = "; secure";} document. cookie = str;}/** obtain Cookie ** cookie_name: cookie key */function getCookie (cookie_name) {var value = null; var allcookies = document. cookie; var cookie_pos = allcookies. indexOf (cookie_name); // If an index is found, the cookie exists. // otherwise, the cookie does not exist. If (cookie_pos! =-1) {// place cookie_pos at the beginning of the value. You only need to add 1 to the value. Cookie_pos + = cookie_name.length + 1; var cookie_end = allcookies. indexOf (";", cookie_pos); if (cookie_end =-1) {cookie_end = allcookies. length;} value = unescape (allcookies. substring (cookie_pos, cookie_end);} return value;}/** Delete Cookie ** cookie_name: cookie key */function delCookie (cookie_name) {var exp = new Date (); exp. setTime (exp. getTime ()-1); var value = getCookie (cookie_name); if (value) {document. cookie = cookie_name + "=" + value + "; expires =" + exp. toGMTString ();}}

I hope this article will help you design javascript programs.

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.