jQuery 操作cookie儲存使用者瀏覽資訊

2022-05-03 15:42:27 字數 1505 閱讀 7416

使用jquery操作cookie之前需要引入jquery的乙個cookie小組件js,**如下:

/*jquery cookie plugins

*/jquery.cookie = function(name, value, options) ;

if (value === null)

var expires = '';

if (options.expires && (typeof options.expires == 'number' || options.expires.toutcstring)) else

expires = '; expires=' + date.toutcstring(); // use expires attribute, max-age is not supported by ie

}var path = options.path ? '; path=' + options.path : '';

var domain = options.domain ? '; domain=' + options.domain : '';

var secure = options.secure ? '; secure' : '';

document.cookie = [name, '=', encodeuricomponent(value), expires, path, domain, secure].join('');

} else }}

return cookievalue;}};

然後開始在頁面寫操作cookie儲存使用者資訊js函式(方法),儲存時間為兩年,例子如下:

// 寫入cookie

function writehistorycookie() ;

var jsoncookie = ;

// parameters value is null? string return

if(!jsoncookie.userid)

//read all history

var objs = $j.cookie('historycooike');

var arraycookies = ;

var jsonobjs = null;

if(objs != null)

//put to first

formatjson.data.unshift(jsoncookie);

//get string type json

jsonobjs = json.stringify(formatjson);

//writer

$j.cookie('historycooike', jsonobjs , );

//writer over show

showhistory();

}// 讀取cookie

function showhistory()

}if(str == '') else

}// 清除cookie

function clearcookie()

Jquery實現Cookie操作

html頁面有的時候也會需要臨時儲存一些狀態資訊,這個時候使用cookie是比較好的,html5畢竟還不多。這裡介紹一下jquery.cookie的用法。說明 jquery.cookie.js 的配置 首先包含jquery的庫檔案,在後面包含 jquery.cookie.js 的庫檔案。使用方法 1...

jQuery操作cookie 的方法

jquery cookie是個很好的cookie外掛程式,大概的使用方法如下 example cookie name value 設定cookie的值,把name變數的值設為value example cookie name value 新建乙個cookie 包括有效期 路徑 網域名稱等 examp...

獲取cookie的過期時間 cookie 儲存

返回類似這種格式物件 tue dec 24 2019 17 12 55 gmt 0800 中國標準時間 var odate new date 年 var curyear odate.getfullyear 月 月份需要 1 var curmonth odate.getmonth 1 日 var cu...