JS sets cookie, reads cookie, deletes cookie, and jscookie
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.
Assume that in A case flow, page A jumps to page B. If JS is used in page A to save the value of A variable with the variable temp, on page B, you also need to use JS to reference the temp variable value. The lifecycle of global or static variables in JS is limited, when a page Jump or the page is closed, the values of these variables are reloaded, that is, they are not saved. The best solution to this problem is to use cookies to save the value of this variable. How can we set and read cookies?
First, you need to take a look at the cookie structure. Simply put, cookies are saved in the form of key-value pairs, that is, the format of key = value. Each cookie is generally separated by a comma.
Set cookie in JS:
Assume that the username value ("jack") of the variable is saved to the cookie and the key value is name, the corresponding JS Code is:
Copy codeThe Code is as follows:
Document. cookie = "name =" + username;
JS cookie reading:
Assume that the content stored in the cookie is: name = jack; password = 123
The JS Code for getting the value of the variable username on page B is as follows:
Var username = document. cookie. split (";") [0]. split ("=") [1]; // JS method for operating cookies! // Write cookiesfunction setCookie (name, value) {var Days = 30; var exp = new Date (); exp. setTime (exp. getTime () + Days * 24x60*60*1000); document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();}
Read cookies
function getCookie(name){var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");if(arr=document.cookie.match(reg))return unescape(arr[2]);elsereturn null;}
Delete cookies
Function 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 the sample setCookie ("name", "hayden"); alert (getCookie ("name ")); // if you need to set the custom expiration time // replace the above setCookie function with the following two functions. // the program code function setCookie (name, value, time) {var strsec = getsec (time); var exp = new Date (); exp. setTime (exp. getTime () + strsec * 1); document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();} function getsec (str) {alert (str); var str1 = str. substring (1, str. length) * 1; var str2 = str. substring (1000); if (str2 = "s") {return str1 *;} else if (str2 = "h ") {return str1 * 60*60*1000;} else if (str2 = "d") {return str1 * 24*60*60*1000 ;}} // This is an example of how to set the expiration time: // s20 indicates 20 S // h indicates the hour. For example, 12 hours indicates that h12 // d indicates the number of days, 30 days: d30setCookie ("name", "hayden", "s20 ");
The above is all the content of this article. I hope you will like it.