Here is not the main way to talk about this method, JS write cookies This code on the Internet, when the use of a bit of a problem, is to write a cookie is a path problem, in the user directory can be used to jump to another directory under the cookie, After comparing the differences between the cookie and other cookies found, the cookie is written in the current directory, not the root directory, here can be used to control access but not meet the current needs, somewhere on the internet to see a piece of code, can solve this problem, posted here, We need to use for reference. The code is as follows:
1Utils.writecookie =function(value, name, key) {2 varDays = 2;3 varExp =NewDate ();4Exp.settime (Exp.gettime () + days * 24 * 60 * 60 * 1000);5 if(Key = =NULL|| Key = = "") {6Document.cookie = name + "=" + encodeURI (value) + "; expires=" + exp.togmtstring () + ";p ath=/";7 }8 Else {9 varNamevalue =GetCookie (name);Ten if(Namevalue = = "") { OneDocument.cookie = name + "=" + key + "=" + encodeURI (value) + "; expires=" + exp.togmtstring () + ";p ath=/"; A } - Else { - varKeyValue =GetCookie (name, key); the if(KeyValue! = "") { -Namevalue = namevalue.replace (key + "=" + keyValue, key + "=" +encodeURI (value)); -Document.cookie = name + "=" + Namevalue + "; expires=" + exp.togmtstring () + ";p ath=/"; - } + Else { -Document.cookie = name + "=" + Namevalue + "&" + key + "=" + encodeURI (value) + "expires=" + exp.togmtstring () + ";p A th=/"; + } A } at } -}
Incidentally, the reading of the cookie has been affixed together, citing the other person's code is not original:
1Utils.readcookie =function(name,key) {2 varNamevalue = "";3 vararr,reg=NewRegExp ("(^|)" +name+ "= ([^;] *)(;|$)");4 if(arr =Document.cookie.match (REG)) {5Namevalue = decodeURI (arr[2]);6 }7 if(Key! =NULL&& Key! = "") {8Reg =NewRegExp ("(^| |&)" + key + "= ([^ (; |&|=)]*) (&|$)");9 if(arr =Namevalue.match (REG)) {TenAlert (decodeURI (arr[2));returndecodeURI (arr[2]); One } A Else return""; - } - Else { the returnNamevalue; - } -}
JS Operation Cookie Cookie path problem