JS set method to get cookies

Source: Internet
Author: User
Tags set cookie split tostring

  This article mainly introduces the JS settings to obtain cookies, a friend in need can refer to the

Combining the JavaScript authority guide with the data collected on the Web during project development, two methods of setting up and getting cookies were sorted out.     Code as follows: <script>  //Set cookie   method one function Setcookie (name,value) {  var exp = new Date ();  Exp.settime (Exp.gettime () + 1*60*60*1000);/valid for 1 hours   Document.cookie = name + "=" + Escape (value) + "; expires=" + Exp.togmtstring (); }  //* When accessing cookies, it is generally necessary to encode the characters that are easily injected, the corresponding decoding when getting cookies, there are many ways of encoding, there is time to write a blog about encoding and decoding/ / /Set Cookie method two direct storage cookies Document.cookie = "homepage = http://www.jb51.net";    /*------------------------------------------------------------------------------------------------ -------/ //Cookies function method One functions GetCookie (name) {  var arr = Document.cookie.match (New RegExp ("(^|)" +name+ "= ([^;] *)(;|$)"));   if (arr!= null)   return unescape (arr[2));   return null;    }  //Fetch Cookies function   Method two function GetCookie (key) {if (key==null) return null; if (Object.prototype.toString.call (key) = = ' [Object String] ' | | ObjecT.prototype.tostring.call (key) = = ' [object number] ') {var arrstr = Document.cookie.split (";");     for (var i= 0;i<arrstr.length;i++) {var temp = arrstr[i].split ("=");     if (Temp[0]==key) return unescape (temp[1]);   return null; return null; }   </script>     When learning a lot of JS methods encountered will not be on the Internet to find information, until mastered.

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.