/**
* Created by 13352 on 2018/7/5.
*/
define(function() {
var 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 {
if (this._cookieCache.hasOwnProperty(key)) {
window.sessionStorage.setItem(key, this._cookieCache[key]);
}
} catch (e) {
console.error(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 = {};
var self = this;
cookies.forEach(function(cookie) {
var kv = cookie.split('=');
if ((new RegExp(self._generatePrefix() + '.+')).test(kv[0]) && kv[1]) {
self._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;
},
del: function(key) {
return this.remove(key);
},
remove: function(key) {
try {
window.sessionStorage.removeItem(key);
} catch (e) {
console.error(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(); return session;
});

  

  config.js

// https://requirejs.org/docs/release/2.1.11/comments/require.js
// https://requirejs.org/docs/release/2.1.11/minified/require.js
require.config({
paths : {
"jquery-1.10.2" : "../assets/js/jquery",
"jquery-ui": "../assets/jqui/jquery-ui",
// "jquery": "../assets/bootstrap/js/jquery-3.3.1.min",
"jquery": "../assets/ckeditor/js/jquery-3.2.1",
"ckeditor-core": "../assets/ckeditor/ckeditor",
'ckeditor-jquery': "../assets/ckeditor/adapters/jquery",
"jquery-cookie": "../assets/js/jquery.cookie",
"bootstrap": "../assets/bootstrap/js/bootstrap",
"alert": "js/lib/alert",
"supersized": "../assets/js/login/supersized.3.2.7",
"url-param": "js/util/getUrlParam",
'image-preview': 'js/util/preview',
'cropbox': "../assets/cropbox/cropbox",
'session': "js/util/session"
},
shim: {
'ckeditor-jquery':{deps:['jquery','ckeditor-core']},
'jquery-cookie': {deps: ['jquery']},
'bootstrap': {deps: ['jquery']},
'jquery-ui':{deps: ['jquery']},
'supersized': {deps: ['jquery']},
'cropbox': {deps: ['jquery']}
}
});

  

  Usage:

require(['session'], function(session) {
session.set('schoolName', '许昌市家里蹲大学');
var schoolName = session.get('schoolName');
console.log(schoolName); // 许昌市家里蹲大学 session.set('a', JSON.stringify({a:1,b:2}));
var a= session.get('a');
console.log(a); // {"a":1,"b":2}
});

  

最新文章

  1. iOS获取网络图片大小
  2. 跟我学 NHibernate (三)
  3. android Init 相关分析
  4. 抛弃EF,20分构建一个属于自己的ORM框架
  5. 关于JDBC链接数据库的代码实现
  6. Classloaders and Classes
  7. Android开发:shape和selector和layer-list的(详细说明)
  8. 激活前一个程序(注册全局消息,使用Mutex探测,如果已经占用就广播消息通知第一个程序,然后第一个程序做出响应)
  9. 20155324 实验5 MSF基础应用
  10. 将已经存在的异步请求callback转换为同步promise
  11. jenkins在windows及linux环境下安装
  12. 1.Python
  13. localStorage,sessionStorage,cookie使用场景和区别
  14. day20re模块 正则表达式
  15. MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY)
  16. runOnUiThread更新主线程
  17. .geodatabase与gdb的相互转换
  18. ARP地址解析协议原理
  19. [Algorithm] Search for matching words
  20. Git之删除仓库

热门文章

  1. Linux部署达梦数据库(完全版)
  2. Java社区——个人项目开发笔记(二)
  3. 【mysql】单表使用索引常见的索引失效
  4. wpf toggleSwitch 的只读属性
  5. EL表达式学习(二)
  6. SpringBoot 优雅配置跨域多种方式及Spring Security跨域访问配置的坑
  7. 高德地图&兴趣点(poi)
  8. C# - 习题02_写出程序的输出结果a.Fun()
  9. 字符型:char
  10. 【Azure 应用服务】Python flask 应用部署在Aure App Service 遇见的 3 个问题