其中 $.extend,$.currDynamicTime 是 jQuery 写法,其他均是 JavaScript 代码。
$.extend({
// get param
getParam: function (_key) {
var _search = location.search;
if (_search == null || _search.length < 1) {
return null;
}
_search = _search.replace('?', '');
var _searchArr = _search.split('&');
for (var _x in _searchArr) {
var _startIndex = _searchArr[_x].indexOf('=');
if (_startIndex < 0) {
continue;
}
var _paramKey = _searchArr[_x].substring(0, _startIndex);
if (_paramKey === _key) {
return _searchArr[_x].substring(_startIndex).replace('=', '');
}
}
return null;
},
// get all param
getAllParam: function () {
var _params = {};
var _search = location.search;
if (_search == null || _search.length < 1) {
return _params;
}
_search = _search.replace('?', '');
var _searchArr = _search.split('&');
for (var _x in _searchArr) {
var _startIndex = _searchArr[_x].indexOf('=');
if (_startIndex < 0) {
continue;
}
var _paramKey = _searchArr[_x].substring(0, _startIndex);
_params[_paramKey] = _searchArr[_x].substring(_startIndex).replace('=', '');
}
return _params;
},
// set cookie
setCookie: function (_key, _value, _expireDays, _path) {
if (typeof _key !== 'string' || _key.trim().length < 1 || !(typeof _value === 'string' || typeof _value === 'number')) {
return;
}
var _str = _key + '=' + _value;
if (typeof _expireDays === 'number' && _expireDays > 0 && /^\d*$/.test(_expireDays + '')) {
var _d = new Date();
_d.setTime(_expireDays * 24 * 60 * 60 * 1000 + _d.getTime());
_str += '; expires=' + _d.toGMTString();
}
if (typeof _path === 'string' && _path.trim().length > 0) {
_str += '; path=' + _path;
}
document.cookie = _str;
},
// get cookie
getCookie: function (_key) {
if (_key == null || _key.trim().length < 1) {
return null;
}
var cookieStr = document.cookie;
if (cookieStr == null || cookieStr.length < 1) {
return null;
}
_key += '=';
var _cookieArr = cookieStr.split(';');
for (var _x in _cookieArr) {
if (_cookieArr[_x].indexOf(_key) === 0) {
return _cookieArr[_x].substring(_key.length);
}
}
return null;
},
// del cookie
delCookie: function (_key, _path) {
if (typeof _key !== 'string' || _key.trim().length < 1) {
return;
}
var _d = new Date();
_d.setTime(_d.getTime() - 10000);
var _str = _key + '=; expires=' + _d.toGMTString();
if (typeof _path === 'string' && _path.trim().length > 0) {
_str += '; path=' + _path;
}
document.cookie = _str;
},
//Executes the current dynamic time
currDynamicTime: function (_ms, _func) {
var _dd = new Date(_ms);
var _year = _dd.getFullYear().toString();
var _month = (_dd.getMonth() + 1).toString();
var _date = _dd.getDate().toString();
var _hours = _dd.getHours().toString();
var _minutes = _dd.getMinutes().toString();
var _seconds = _dd.getSeconds().toString();
if (_month.length === 1) {
_month = '0' + _month;
}
if (_date.length === 1) {
_date = '0' + _date;
}
if (_hours.length === 1) {
_hours = '0' + _hours;
}
if (_minutes.length === 1) {
_minutes = '0' + _minutes;
}
if (_seconds.length === 1) {
_seconds = '0' + _seconds;
}
_func(_year, _month, _date, _hours, _minutes, _seconds);
setTimeout(function () {
$.currDynamicTime(_ms + 1000, _func);
}, 1000);
}
});
网友评论