There are many methods for setting cookies in js, including setting cookies in JS and reading cookies, which are often used in work! The following is the detailed code. If you are interested, you can refer to the JavaScript script running on the client. Therefore, you cannot set the Session 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
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 ();} // example setCookie ('username', 'darren', 30) alert (getCookie ("username"); script
Example 3