JavaScript Cookie Operations Detailed introduction

Source: Internet
Author: User
Tags setcookie

Keep in mind that cookies are a string property of a document. To save a cookie, simply create a string, the format is name=<value> (name = value), and then set the document's Document.cookie to be equal to it. For example, if you want to save the user name that the form receives, the code looks like this:

The code is as follows Copy Code

Document.cookie = "username" + Escape (form.username.value);

It is important to use the escape () function here because the cookie value may contain semicolons, commas, or spaces. This means that when reading the cookie value, the corresponding unescape () function must be used to decode the value.

We also have to introduce four properties of cookies. These attributes are appended to the string value in the following format:

The code is as follows Copy Code

name=<value>[; expires=<date>][; domain=<domain>][; path=<path>][; Secure]

Name =< value >[; expires=< date >][; domain=< domain >][; path=< path >][; security]

<value>, <date>, <domain> and <path> should be replaced with corresponding values. <date> You should use the GMT format to get the date value of this GMT format using the date class dated. toGMTString () method of the JavaScript scripting language. The square brackets represent this item to be optional. For example, the square brackets on both sides of the [; secure] will need to be set to make the cookie safe; Secure "is appended to the cookie string value. If "; Secure "is not added to the cookie string, then this cookie is not secure. Do not add angle brackets <> and brackets [] to cookies (unless they are the contents of certain values). When you set properties, you do not have properties that can be set in any order.


1, GetCookie (name)

Gets the cookie value, the parameter name is the cookie name.

2, Setcookie (name, value)

Set the cookie value to set the cookie value for name.

3, Deletecookie (name)

Deletes the cookie value for name.

The code is as follows Copy Code

/** Get Cookies */

Get the value of a cookie
The following describes how to get the value of a cookie. The value of a cookie can be obtained directly by Document.cookie:
var Strcookie=document.cookie;
This will get a string of multiple name/value pairs separated by semicolons that include all cookies under that domain name. For example:
<script language= "JavaScript" type= "Text/javascript" >
<!--
Document.cookie= "userid=828";
Document.cookie= "Username=hulk";
var Strcookie=document.cookie;
alert (Strcookie);
-->
</script>

function GetCookie (name) {
var arg = name + "=";
var alen = Arg.length;
var clen = document.cookie.length;
var i = 0;
while (I < Clen) {
var j = i + Alen;
if (Document.cookie.substring (i, j) = = arg) return Getcookieval (j);
i = Document.cookie.indexOf ("", I) + 1;
if (i = = 0) break;
}
return null;
}
/** Set Cookies */
function Setcookie (name, value) {
var argv = setcookie.arguments;
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 ":" ");
}
/** Delete Cookies */
function Deletecookie (name) {
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval = 0;
Document.cookie = name + "=" + Cval + "; Expires= "+ exp.togmtstring ();
}
function Getcookieval (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr = = 1) endstr = document.cookie.length;
Return unescape (document.cookie.substring (offset, endstr));
}

Related Article

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.