溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

jquery中如何使用session

發(fā)布時間:2020-12-04 11:50:42 來源:億速云 閱讀:580 作者:小新 欄目:web開發(fā)

小編給大家分享一下jquery中如何使用session,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

在jquery中能用session,使用方法為:1、添加數(shù)據(jù),代碼為【$.session.set('key', 'value')】;2、刪除數(shù)據(jù),代碼為【$.session.remove('key')】。

在jquery中能用session,使用方法為:

添加數(shù)據(jù)

$.session.set('key', 'value')

刪除數(shù)據(jù)

$.session.remove('key');

獲取數(shù)據(jù)

$.session.get('key');

清除數(shù)據(jù)

$.session.clear();

以下為jquery文件代碼:新建復制即可使用

/** *說明:不可以獲取java的session* 語法:添加數(shù)據(jù)$.session.set('key', 'value')
刪除數(shù)據(jù)$.session.remove('key');
獲取數(shù)據(jù)$.session.get('key');
清除數(shù)據(jù)$.session.clear();*/
(function($){
$.session = {
_id: null,
_cookieCache: undefined,
_init: function(){if (!window.name) {window.name = Math.random();}this._id = window.name;this._initCache();
// See if we've changed protcols
var matches = (new RegExp(this._generatePrefix() + "=([^;]+);")).exec(document.cookie);if (matches && document.location.protocol !== matches[1]) {this._clearSession();for (var key in this._cookieCache) {try {window.sessionStorage.setItem(key, this._cookieCache[key]);} catch (e) {};}}
document.cookie = this._generatePrefix() + "=" + document.location.protocol + ';path=/;expires=' + (new Date((new Date).getTime() + 120000)).toUTCString();
},
_generatePrefix: function(){return '__session:' + this._id + ':';},
_initCache: function(){var cookies = document.cookie.split(';');this._cookieCache = {};for (var i in cookies) {var kv = cookies[i].split('=');if ((new RegExp(this._generatePrefix() + '.+')).test(kv[0]) && kv[1]) {this._cookieCache[kv[0].split(':', 3)[2]] = kv[1];}}},
_setFallback: function(key, value, onceOnly){var cookie = this._generatePrefix() + key + "=" + value + "; path=/";if (onceOnly) {cookie += "; expires=" + (new Date(Date.now() + 120000)).toUTCString();}document.cookie = cookie;this._cookieCache[key] = value;return this;},
_getFallback: function(key){if (!this._cookieCache) {this._initCache();}return this._cookieCache[key];},
_clearFallback: function(){for (var i in this._cookieCache) {document.cookie = this._generatePrefix() + i + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';}this._cookieCache = {};},
_deleteFallback: function(key){document.cookie = this._generatePrefix() + key + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';delete this._cookieCache[key];},
get: function(key){return window.sessionStorage.getItem(key) || this._getFallback(key);},
set: function(key, value, onceOnly){try {window.sessionStorage.setItem(key, value);} catch (e) {}this._setFallback(key, value, onceOnly || false);return this;},'delete': function(key){return this.remove(key);},
remove: function(key){try {window.sessionStorage.removeItem(key);} catch (e) {};this._deleteFallback(key);return this;},
_clearSession: function(){try {window.sessionStorage.clear();} catch (e) {for (var i in window.sessionStorage) {window.sessionStorage.removeItem(i);}}},
clear: function(){this._clearSession();this._clearFallback();return this;}
};
$.session._init();
})(jQuery);

看完了這篇文章,相信你對jquery中如何使用session有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI