Set cookie and read cookie in JS, and set cookie read in js

Source: Internet
Author: User
Tags set cookie setcookie

Set cookie and read cookie in JS, and set cookie read in js

JavaScript is a script running on the client. Therefore, the Session cannot be set because the Session is running on the server.

The cookie runs on the client, so you can use JS to set the cookie.

Summary of cookie setting methods in js:

First:

<Script> // set cookiefunction setCookie (cname, cvalue, exdays) {var d = new Date (); d. setTime (d. getTime () + (exdays * 24x60*60*1000); var expires = "expires =" + d. toUTCString (); document. cookie = cname + "=" + cvalue + ";" + expires;} // obtain cookiefunction getCookie (cname) {var name = cname + "="; var ca = document. cookie. split (';'); for (var I = 0; I <ca. length; I ++) {var c = ca [I]; while (c. charAt (0) = '') C = c. substring (1); if (c. indexOf (name )! =-1) return c. substring (name. length, c. length);} return "" ;}// clear cookie function clearCookie (name) {setCookie (name, "",-1);} function checkCookie () {var user = getCookie ("username"); if (user! = "") {Alert ("Welcome again" + user);} else {user = prompt ("Please enter your name:", ""); if (user! = "" & User! = Null) {setCookie ("username", user, 365) ;}} checkCookie (); </script>

Second:

<Script> // JS method for operating cookies! // Write cookiesfunction setCookie (c_name, value, expiredays) {var exdate = new Date (); exdate. setDate (exdate. getDate () + expiredays); document. cookie = c_name + "=" + escape (value) + (expiredays = null )? "": "; Expires =" + exdate. toGMTString ();} // read cookiesfunction getCookie (name) {var arr, reg = new RegExp ("(^ |)" + name + "= ([^;] *) (; | $) "); if (arr = document. cookie. match (reg) return (arr [2]); else return null;} // Delete cookiesfunction delCookie (name) {var exp = new Date (); exp. setTime (exp. getTime ()-1); var cval = getCookie (name); if (cval! = Null) document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString () ;}// use example setCookie ('username', 'darren', 30) alert (getCookie ("username"); </script>

Example 3

<Html> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> 

Note:

Chrome cannot obtain the cookie locally. It must be on the server. If it is local, you can put it under the local www directory.

Google Chrome only supports reading and writing cookies on online websites, and does not allow cookie operations on local html. Therefore, if you write the following code in a local html file, the content of the pop-up dialog box is blank.

document.cookie = "Test=cooo";alert(document.cookie);

If this page is the content of an online website, the cookie content Test = cooo will be displayed normally.

The above is all the content of this article. I hope you will like it.

Articles you may be interested in:
  • Articles on cookie operations in javascript (setting and deleting cookies)
  • Php reads the cookie code set in javascript
  • Set, read, and delete instance code using javascript cookies
  • ASP. NET cannot obtain the method for setting cookies in JS.
  • Js sets cookie expiration and clears the cookie with the corresponding browser name
  • Httpclient simulated login implementation (set cookie using js)
  • How to get cookies in JS settings
  • How to Get and delete JavaScript cookie settings
  • Js sets the current cookie expiration time minus one second, which is equivalent to immediate expiration.
  • Set, read, and delete cookies in JS

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.