首頁  >  文章  >  web前端  >  在jquery中能用session嗎

在jquery中能用session嗎

coldplay.xixi
coldplay.xixi原創
2020-12-01 15:19:093416瀏覽

在jquery中能用session,使用方法為:1、新增數據,程式碼為【$.session.set('key', 'value')】;2、刪除數據,程式碼為【$ .session.remove('key')】。

在jquery中能用session嗎

本教學操作環境:windows7系統、jquery3.2.1版本,此方法適用於所有品牌電腦。

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

#新增資料

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

刪除資料
##

$.session.remove('key');

取得資料


$.session.get('key');

清除資料


$.session.clear();

以下為jquery檔案代碼:新複製即可使用

/** *说明:不可以获取java的session* 语法:添加数据$.session.set('key', 'value')
删除数据$.session.remove('key');
获取数据$.session.get('key');
清除数据$.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);

相關免費學習推薦:

javascript(影片)

以上是在jquery中能用session嗎的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn