쿠키와 하위 쿠키 조작에 대한 봉인

9289 단어 cookie
  1 /**

  2  *   cookie   

  3  * @type {Object}

  4  */

  5 var CookieUtil = {

  6     /**

  7      *   cookie         

  8      * @param name cookie  

  9      * @return {*}

 10      */

 11     get:function (name) {

 12         // name  URL  

 13         var cookieName = encodeURIComponent(name) + '=',

 14             cookieStart = document.cookie.indexOf(cookieName),

 15             cookieValue = null;

 16         //  cookieName

 17         if (cookieStart > -1) {

 18             // cookieStart        ";"

 19             var cookieEnd = document.cookie.indexOf(';', cookieStart);

 20             //   ";",  document.cookie      

 21             if (cookieEnd === -1) {

 22                 cookieEnd = document.cookie.length;

 23             }

 24             //    value  

 25             cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));

 26         }

 27         //  

 28         return cookieValue;

 29     },

 30     /**

 31      *     cookie

 32      * @param name cookie  

 33      * @param value     

 34      * @param expire      Date

 35      * @param path   

 36      * @param domain   

 37      * @param secure Boolean

 38      */

 39     set:function (name, value, expire, path, domain, secure) {

 40         //     URL  

 41         var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);

 42         if (expire instanceof Date) {

 43             //toGMTString()       Date  

 44             cookieText += '; expire=' + expire.toGMTString();

 45         }

 46         if (path) {

 47             cookieText += '; path=' + path;

 48         }

 49         if (domain) {

 50             cookieText += '; domain=' + domain;

 51         }

 52         if (secure) {

 53             cookieText += '; secure';

 54         }

 55         document.cookie = cookieText;

 56     },

 57     /**

 58      *   cookie

 59      * @param name

 60      * @param path

 61      * @param domain

 62      * @param secure

 63      */

 64     unset:function (name, path, domain, secure) {

 65         this.set(name, '', new Date(0), path, domain, secure);

 66     }

 67 };

 68 /*

 69  //set cookies

 70  CookieUtil.set(“name”, “Nicholas”);

 71  CookieUtil.set(“book”, “Professional JavaScript”);

 72  //read the values

 73  alert(CookieUtil.get(“name”)); //”Nicholas”

 74  alert(CookieUtil.get(“book”)); //”Professional JavaScript”

 75  //remove the cookies

 76  CookieUtil.unset(“name”);

 77  CookieUtil.unset(“book”);

 78  //set a cookie with path, domain, and expiration date

 79  CookieUtil.set(“name”, “Nicholas”, “/books/projs/”, “www.wrox.com”,

 80  new Date(“January 1, 2010”));

 81  //delete that same cookie

 82  CookieUtil.unset(“name”, “/books/projs/”, “www.wrox.com”);

 83  //set a secure cookie

 84  CookieUtil.set(“name”, “Nicholas”, null, null, null, true);

 85  */

 86 

 87 // cookie   

 88 //             cookie   

 89 // cookie      cookie        

 90 var SubCookieUtil = {

 91     /**

 92      *      cookie  

 93      * @param name cookie  

 94      * @param subName  cookie  

 95      * @return {*}

 96      */

 97     get:function (name, subName) {

 98         var subCookies = this.getAll(name);

 99         if (subCookies) {

100             return subCookies(subName);

101         } else {

102             return null;

103         }

104     },

105     /**

106      *      cookie             

107      * @param name cookie  

108      * @return {*}      null

109      */

110     getAll:function (name) {

111         var cookieName = encodeURIComponent(name) + "=",

112             cookieStart = document.cookie.indexOf(cookieName),

113             cookieValue = null,

114             result = {};

115         if (cookieStart > -1) {

116             var cookieEnd = document.cookie.indexOf(";", cookieName);

117             if (cookieEnd === -1) {

118                 cookieEnd = document.cookie.length;

119             }

120             //      ,     cookie    

121             cookieValue = document.cookie.substring(cookieStart + cookieName.length, cookieEnd);

122             //

123             if (cookieValue.length > 0) {

124                 //    cookie   

125                 var subCookies = cookieValue.split("&");

126                 //      cookie     ,       

127                 for (var i = 0, len = subCookies.length; i < len; i++) {

128                     var parts = subCookies[i].split("=");

129                     result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);

130                 }

131                 return result;

132             }

133         }

134         //   ,   

135         return null;

136     },

137     /**

138      *      cookie

139      * @param name cookie  

140      * @param subName  cookie  

141      * @param value  cookie 

142      * @param expires     

143      * @param path   

144      * @param domain   

145      * @param secure Boolean     

146      */

147     set:function (name, subName, value, expires, path, domain, secure) {

148         //  name      cookie,        

149         var subcookies = this.getAll(name) || {};

150         //         

151         subcookies[subName] = value;

152         //  setAll()  

153         this.setAll(name, subcookies, expires, path, domain, secure);

154     },

155     /**

156      *      cookie

157      * @param name cookie  

158      * @param subcookies  cookie  

159      * @param expires     

160      * @param path   

161      * @param domain   

162      * @param secure Boolean     

163      */

164     setAll:function (name, subcookies, expires, path, domain, secure) {

165         var cookieText = encodeURIComponent(name) + "=";

166         //    ,     cookie

167         var subcookieParts = [];

168         //      

169         for (var subName in subcookies) {

170             //    ,               

171             if (subName.length > 0 && subcookies.hasOwnProperty(subName)) {

172                 subcookieParts.push(encodeURIComponent(subName) + "=" + encodeURIComponent(subcookies[subName]));

173             }

174         }

175         //   cookie

176         if (subcookieParts.length > 0) {

177             //   cookie

178             cookieText += subcookieParts.join("& ");

179             if (expires instanceof Date) {

180                 cookieText += ";expires=" + expires.toGMTString();

181             }

182             if (path) {

183                 cookieText += ";path=" + path;

184             }

185             if (domain) {

186                 cookieText += ";domain=" + domain;

187             }

188             if (secure) {

189                 cookieText += ";secure";

190             }

191         } else {

192             //     cookie  

193             cookieText += ";expires=" + (new Date(0)).toGMTString();

194         }

195         document.cookie = cookieText;

196     },

197     /**

198      *      cookie     

199      * @param name

200      * @param subName

201      * @param path

202      * @param domain

203      * @param secure

204      */

205     unset:function (name, subName, path, domain, secure) {

206         var subcookies = this.getAll(name);

207         if (subcookies) {

208             //         

209             delete subcookies[subName];

210             //    cookie

211             this.setAll(name, subcookies, null, path, domain, secure);

212         }

213     },

214     /**

215      *      cookie

216      * @param name

217      * @param path

218      * @param domain

219      * @param secure

220      */

221     unsetAll:function (name, path, domain, secure) {

222         this.setAll(name, null, new Date(0), path, domain, secure);

223     }

224 };

225 /*

226  //assume document.cookie=data=name=Nicholas&book=Professional%20JavaScript

227  //get all subcookies

228  var data = SubCookieUtil.getAll(“data”);

229  alert(data.name); //”Nicholas”

230  alert(data.book); //”Professional JavaScript”

231  //get subcookies individually

232  alert(SubCookieUtil.get(“data”, “name”)); //”Nicholas”

233  alert(SubCookieUtil.get(“data”, “book”)); //”Professional JavaScript”

234 

235  //assume document.cookie=data=name=Nicholas&book=Professional%20JavaScript

236  //set two subcookies

237  SubCookieUtil.set(“data”, “name”, “Nicholas”);

238  SubCookieUtil.set(“data”, “book”, “Professional JavaScript”);

239  //set all subcookies with expiration date

240  SubCookieUtil.setAll(“data”, { name: “Nicholas”, book: “Professional JavaScript” },

241  new Date(“January 1, 2010”));

242  //change the value of name and change expiration date for cookie

243  SubCookieUtil.set(“data”, “name”, “Michael”, new Date(“February 1, 2010”));

244 

245  //just remove the “name” subcookie

246  SubCookieUtil.unset(“data”, “name”);

247  //remove the entire cookie

248  SubCookieUtil.unsetAll(“data”);

249  */

좋은 웹페이지 즐겨찾기