第03天浏览器
# HTML加meta
<meta http-equiv="Expires" content="Mon, 20 Jul 2016 28:88:00 GMT" />
<meta http-equiv="Cache-Control" content="max-age=7200"/>
复制代码
# 服务端
const static = require('koa-static')
const app = koa()
app.use(static('./pages', {
maxage:7200
}))
复制代码
# 安全封装
let rkey = /^[0-9A-Za-z_@-]*$/
let store
function init(){
if(typeof store === 'undefined') {
store = window['localStorage']
}
return true;
}
function isValidKey(key){
if (typeof key !== 'string') {
return false
}
return rkey.test(key)
}
exports = {
set(key, value){
let success = false
if (isValidKey(key) && init()) {
try {
value += ''
store.setItem(key, value)
success = true
} catch (e) {}
}
return success
},
get(key) {
if (isValidKey(key) && init()){
try{
return store.getItem(key)
} catch (e) {}
}
return null
},
remove(key){
if (isValidKey(key) && init()){
try{
return store.removeItem(key)
return true
} catch (e) {}
}
return false
},
clear(){
if (init()){
try{
for (let key in store) {
store.removeItem(key)
}
return true
} catch (e) {}
}
return false
}
}
module.exports = exports
复制代码
# 设置多个子域中共享Cookies
this.cookies.set('username', 'ouven', {
domain: '.domain.com',
path: '/'
})
复制代码
# 封装
exports = {
get(n){
let m = document.cookie.match(new RegExp( "(^| )"+n+"=([^;]*)(;|$)" ))
return !m ? '' : decodeURIComponent(m[2])
},
set(name, value, domain, path, hour){
let expire = new Date()
expire.setTime(expire.getTime() + (hour ? 3600000 * hour : 30*24*60*60*1000))
document.cookie = name + '=' + value + ';' + 'expires=' + expire.toGMTString() + ';path=' + (path ? path : '/') + '; ' + (domain ? ('domain=' + domain' + ';') : '') }, del (name, domain, path) { document.cookie = name + '=; expires=Mon, 26 Jul 1997 05:00:00 GMT; path=' + (path ? path : '/') + '; ' + (domain ? ('domain=' + domain + ';') : '') }, clear() { let rs = document.cookie.match(new RegExp("([^;][^;]*)(?=(=[^;]*)(;|$))", 'gi')) for (let i in rs) { document.cookie = rs[i] + '=;expires=Mon, 26 Jul 1997 05:00:00 GMT; path=/;' } } } module.exports = exports 复制代码