A lot of problems were discovered today when the review new JavaScript code was written. Here is an example of function GetCookie (name) {}.
One of the more typical questions is how to get a value from a cookie using JavaScript. So let's start by looking at what the cookie looks like.
Enter directly in the browser address bar: Javascript:alert (Document.cookie); Enter. (This line of code means to have the browser execute a JavaScript statement: alert (document.cookie);)
The result: uin=webryan; sessionid=10293123834; pgv_send=1; cur_page=index
this looks.
Note: 1. There is no space at the beginning, 2. Semicolon followed by a space 3. There is no semicolon at the end. So the way we get cookies is quite clear.
One is to use Document.cookie.split (";") Way to split a string into segments and then iterate through the entire array. Compares each array unit equal to the name on the left side of the equality, and equals the value to the right of the equals sign.
View Plaincopy to Clipboardprint?
- function GetCookie (name) {
- var arr = document.cookie.split (";");
- for (var i=0,len=arr.length;i<len;i++) {
- var item = arr[i].split ("=");
- if (item[0]==name) {
- return item[1];
- }
- }
- return "";
- }
[JS]View PlainCopy
- function GetCookie (name) {
- var arr = document.cookie.split (";");
- For (var i=0,len=arr.length;i<len;i++) {
- var item = arr[i].split ("=");
- if (item[0]==name) {
- return item[1];
- }
- }
- return "";
- }
The second is to search for the keyword directly in the string. Because the semicolon has a space, plus prevents the search cookie from appearing, the value of "str" is a TEST_STR cookie name. Here we do a filter first. And then find out where Str is, so there's no problem. The specific situation is as follows
View Plaincopy to Clipboardprint?
- function getcookie (name) {
- var value=
- var cookie = ";" +document.cookie.replace (/;\s+/g,
- var pos = cookie.indexof ( ";") +name+if (pos>-1) {
- var start = Cookie.indexof (var end = cookie.indexof (
- value = unescape (cookie.substring (start+1,end));
-
- return value;
-
[JS]View PlainCopy
- function GetCookie (name) {
- var value="";
- var cookie = ";" +document.cookie.replace (/;\s+/g,";") +";"
- var pos = cookie.indexof (";" +name+"=");
- if (pos>-1) {
- var start = Cookie.indexof ("=", POS);
- var end = Cookie.indexof (";", start);
- Value = Unescape (cookie.substring (start+1,end));
- }
- return value;
- }
Different methods have different choices at different times. This is Xiao Xiao said the type of problem.
The code that is actually used now is
View Plaincopy to Clipboardprint?
- /**
- * Cookie-related
- */
- $.cookie = {
- /**
- * Read cookies
- *
- * @param {String} n= name
- * @return {String} cookie Value
- * @example
- * $.cookie.get (' id_test ');
- */
- Get:function (n) {
- var m = document.cookie.match (new RegExp ( "(^|)") +n+"= ([^;] *)(;|$)"));
- Return!m? "": unescape (M[2]);
- },
- /**
- * Set Cookies
- * @param {String} name Cookie name-Required
- * @param {String} value cookie value-Required
- * @param domain name of {String} domain
- * @param the path of {String} path
- * @param {Number} hour survival time, unit: Hours
- * @example
- * $.cookie.set (' value1 ', ' cookieval ', "id.qq.com", "/test", 24); Set cookies
- */
- Set:function (name,value,domain,path,hour) {
- var expire = new Date ();
- Expire.settime (Expire.gettime () + (hour?3600000 * hour:30*24*60*60*1000));
- Document.cookie = name + " =" + value + ";" + "expires=" + expire.togmtstring () + "; Path= "+ (Path Path:"/") + "; "+ (domain? ("domain=" + domain + ";"): "");
- },
- /**
- * Delete the specified cookie, the replication is expired!! Note that path is strictly matched,/id is different from/id/
- *
- * @param {String} name Cookie name
- * @param {String} domain
- * @param the path of {String} path
- * @example
- * $.cookie.del (' id_test '); Delete Cookies
- */
- Del: function (name, domain, path) {
- Document.cookie = name + "=; Expires=mon, 1997 05:00:00 GMT; Path= "+ (Path Path:"/") + "; "+ (domain? ("domain=" + domain + ";"): "");
- },
- /**
- * Delete all Cookies--the cookie is not included in this directory for the time being
- * @example
- * $.cookie.clear (); Delete all Cookies
- */
- Clear:function () {
- var rs = document.cookie.match (new RegExp ("([^;] [^;] *)(?=(=[^;] *) (; |$)) ", " GI " ));
- Delete all Cookies
- for (var i in rs) {
- Document.cookie = rs[i] + "=;expires=mon, Jul 1997 05:00:00 GMT; path=/; " ;
- }
- },
- /**
- * UIn-for business, external open source please delete
- *
- * @return {String} UIn value
- * @example
- * $.cookie.uin ();
- */
- UIn:function () {
- var u = $.cookie.get ("UIn");
- Return!u? Null:parseint (u.substring (1, u.length), ten);
- }
- };
Example and simple analysis of using JS to get cookie