操作cookies
一直不是很明白有关客户端cookies的相关内容,只是觉得它可以很方便的保存一些登录的信息等,方便用户下次不需要输入太多的东西。今天有兴趣看了一些有关对cookie操作的一些javascript脚本,了解了一些内容,其实应用也还是蛮简单的。开始我想找到这些文件到底存在什么地方,一搜索,乖乖,竟让我找到好多的cookies.js的脚本。一看在我的机器中jakarta-tomcat-5.0.19下就有,大喜,看之。。。
[*]// =========================================================================
[*]// Cookie functions
[*]// =========================================================================
[*]/* This function is used to set cookies */
[*]function setCookie(name,value,expires,path,domain,secure) {
[*]document.cookie = name + "=" + escape (value) +
[*] ((expires) ? "; expires=" + expires.toGMTString() : "") +
[*] ((path) ? "; path=" + path : "") +
[*] ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
[*]}
[*]
[*]/* This function is used to get cookies */
[*]function getCookie(name) {
[*] var prefix = name + "="
[*] var start = document.cookie.indexOf(prefix)
[*]
[*] if (start==-1) {
[*] return null;
[*] }
[*]
[*] var end = document.cookie.indexOf(";", start+prefix.length)
[*] if (end==-1) {
[*] end=document.cookie.length;
[*] }
[*]
[*] var value=document.cookie.substring(start+prefix.length, end)
[*] return unescape(value);
[*]}
[*]
[*]/* This function is used to delete cookies */
[*]function deleteCookie(name,path,domain) {
[*]if (getCookie(name)) {
[*] document.cookie = name + "=" +
[*] ((path) ? "; path=" + path : "") +
[*] ((domain) ? "; domain=" + domain : "") +
[*] "; expires=Thu, 01-Jan-70 00:00:01 GMT";
[*]}
[*]}
期间有很多的参数可以进行设置,如果没有内容的话,就默认为空。
下面是一个期望的时间转换函数
[*]// utility function to retrieve an expiration data in proper format;
[*] function getExpDate(days, hours, minutes)
[*] {
[*] var expDate = new Date();
[*] if(typeof(days) == "number" && typeof(hours) == "number" && typeof(hours) == "number")
[*] {
[*] expDate.setDate(expDate.getDate() + parseInt(days));
[*] expDate.setHours(expDate.getHours() + parseInt(hours));
[*] expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
[*] return expDate.toGMTString();
[*] }
[*] }
页:
[1]