Js Cookies

 1 function getCookie(name) {

 2     var arr = document.cookie.match(new RegExp("(^| )userInfo=([^;]*)(;|$)"));

 3     if (arr != null) {

 4         arr = unescape(arr[2]).split("&");

 5         for (x in arr)

 6             if (arr[x].split("=")[0] == name) {

 7                 return arr[x].split("=")[1];

 8             }

 9     }

10     else {

11         return "";

12     }

13 }

14 alert( getCookie("userName"))

 

 1 function getCookie(key, name) {

 2     var arr = document.cookie.match(new RegExp("(^| )" + key + "=([^;]*)(;|$)"));

 3     if (arr != null) {

 4         arr = unescape(arr[2]).split("&");

 5         for (x in arr) {

 6             if (arr[x].split("=")[0] == name) {

 7                 return arr[x].split("=")[1];

 8             }

 9         }

10     }

11     return "";

12 }

13 alert(getCookie("userInfo", "userName"))

14 alert(getCookie("userInfo", "lastVisit"))

 

 

 

你可能感兴趣的:(Cookies)