Setting the value of a cookie in JavaScript

Source: Internet
Author: User
Tags server memory

Cookies and sessions are common information storage methods in Web development. A cookie is a place on the client that can store user information. A session is a place where user information is stored in server memory. JavaScript is a script that runs on the client side, so it is generally not possible to set the session because the session is running on the server and the cookie is running on the client, so you can use JS to set the Cooki E.

In the Itoo project, the system needs to pass the value between the pages, now assume that page a, and page B, page B needs a parameter of page A, before the operation can be done, then we need to save the page a parameter value to the cookie, page B through the JS to get the parameter value, for subsequent operations.

JS Write Cookie code:

<span style= "FONT-SIZE:18PX;" >//two parameters, one is the name of the cookie, and the other is the valuefunctionSetcookie (name, value) {//Define a day    varDays = 1; varExp =NewDate (); //define the expiration time,Exp.settime (Exp.gettime () + days * 24 * 60 * 60 * 1000); //writes a cookie, toGMTString converts the time into a string. Document.cookie = name + "=" + Escape (value) + "; expires=" +exp.togmtstring; }</span>

The escape method returns a string value (in Unicode format) that contains the charstring content. All spaces, punctuation, accent marks, and other non-ASCII characters are replaced with%XX encoding, where xx equals the hexadecimal number that represents the character. For example, a space returns "%20". stored in%uxxxx format with a character value greater than 255

JS Read Cookie Code

 <span style= "FONT-SIZE:18PX;" >//  read cookies  function    GetCookie (name) { //  match field  var  arr,reg=new  RegExp ("(^|)" +name+ "= ([^;]                 *) (; |$) ");  if  (Arr=document.cookie.match (reg)) return  (Arr[2 else  return  null  ; }  </span> 

A set of cookie values that may be preceded by a name=value,name (^) or a space (), a match beginning or a space is "(^|)"
Then match name so yes "(^|)" +name, then matches the equals sign so it's "(^|)" +name+ "=,
Then the matching value,value itself ends with a semicolon, so here is a string that is not a semicolon, and the regular is [^;] * And here is the
"(^| )" +name+ "= ([^;] *), the end of the last value is a semicolon or the end of the string, then yes (; |$)
At this point, the entire matching cookie is completed, which is "(^|)" +name+ "= ([^;] *)(;|$)"

In addition to writing cookies in JS, we can also write values into cookies in Contraller.

The cookie method is written as follows:

[JavaScript]View PlainCopy
  1. <span style="FONT-SIZE:18PX;" > //Depositing papers and course IDs in a cookie
  2. HttpCookie cookie = new HttpCookie ("Paperid");
  3. Cookies.  Values.add ("Paperid", exampaperid.tostring ());
  4. Cookies.  Values.add ("Consorid", Exampaper.courseid);
  5. //Add to Browser
  6. Response.appendcookie (Cookie);</span>

The method of reading cookies is:

[JavaScript]View PlainCopy
    1. <span style="FONT-SIZE:18PX;"  >httpcookie Newcookie = request.cookies["Paperid"];
    2. Defining variables for receiving quiz ID
    3. String strpaperid="";
    4. if (Newcookie! = null)
    5. {
    6. Strpaperid = newcookie.values["Paperid"];
    7. }</span>

Setting the value of a cookie in JavaScript

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.