JavaScript to modify cookies to check the custom function

Source: Internet
Author: User

JavaScript Write cookies

The code is as follows Copy Code

function Setcookie (C_name,value,expiredays,path) {
var exdate=new Date ();
Exdate.setdate (Exdate.getdate () +expiredays);
document.cookie=c_name+ "=" +escape (value) + ((expiredays==null)? "": "; expires=" +exdate.togmtstring ()) + ";" + ((path==null)? "": "Path=" + Escape (path));
}
Setcookie ("Qiufeng", "Www.111cn.net", 24);

JavaScript Read cookies

  code is as follows copy code

function GetCookie (c_name) {
if (document.cookie.length>0) {
C_start=document.cookie.indexof (c_name + "=");
if (c_start!=-1) {
C_start=c_start + c_name.length+1;
C_end=document.cookie.indexof (";", C_start);
if (c_end==-1) c_end=document.cookie.length;
Return unescape (document.cookie.substring (c_start,c_end));
}
}
Return "";
}

JavaScript Delete Cookies

The code is as follows Copy Code

function Delcookie (name) {
var date = new Date ();
Date.settime (Date.gettime ()-10000);
Document.cookie = name + "=A;" Expires= "+ date.togmtstring () +";p ath=* ";
}

JavaScript clears all Cookies

The code is as follows Copy Code

function Removecookie () {
var cookie = document.cookie.split (";");
for (var i in cookie) {
Delcookie (Cookie[i].substring (cookie[i].indexof ("="), 0));
}
}

Note: For the two-level domain name is a cookie can not be set or deleted, we can set the domain name 111cn.net can be.

Setcookie ("Qiufeng", "111cn.net", 24);

This can be read in such as qq.111cn.net domain name to this cookie oh, if set to

Setcookie ("Qiufeng", "Www.111cn.net", 24);

Then qq.111cn.net domain name can not access this cookie Oh, the same delete and query is the same OH.

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.