1、动态加载js || css || style
const loadRes = function(name, type, fn) {
let ref;
if (type === 'js') {
ref = document.createElement('script');
ref.setAttrbute('type', 'text/javascript');
ref.setAttrbute('src', name);
} else if (type === 'css') {
ref = document.createElement('link');
ref.setAttrbute('rel', 'stylesheet');
ref.setAttrbute('type', 'text/css');
ref.setAttrbute('href', name);
} else if (type === 'style') {
ref = document.createElement('style');
ref.innerhtml = name;
}
if (typeog ref !== 'undefined') {
document.getElementsByTagName('head')[0].appendChild(ref);
ref.onload = function() {
typeof fn === 'function' && fn()
}
}
}
2、获取url参数
const getUrlParams = function(name) {
let reg = new RegExp('(^|&?)' + name + '=([^&]*)(&|$)', 'i');
let r = window.location.href.substr(1).match(ref);
if (r != null) {
return decodeURI(r[2]);
}
return undefined;
}
3、本地存储
const store = {
set: function(name, value, exp) {
let d = new Date();
let time = 0;
exp = (typeof(exp) === 'undefined' || !exp) ? 1: exp;
time = d.setHours(d.getHours() + (24 * exp));
localStorage.setItem(name, JSON.stringfy({data: value, time: time}));
},
get: function(name) {
let data = localStorage.getItem(name);
if (!data) {
return null;
}
let obj = JSON.parse(data);
if (new Date().getTime() > obj.time) {
localStorage.removeItem(name);
return null;
} else {
return obj.data;
}
},
clear: function(name) {
if (name) {
localStorage.removeItem(name);
} else {
localStorage.clear();
}
}
}