好用的公共js
function getDate(date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
return year + "年-" + month + "月-" + day+'日' ;
}
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]); return null; //返回参数值
}
//时间格式化
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
formatterDate = function(date) {
var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
+ (date.getMonth() + 1);
return date.getFullYear() + '-' + month + '-' + day;
};
function getContext() {
var pathName = document.location.pathname;
var index = pathName.substr(1).indexOf("/");
var result = pathName.substr(0,index+1);
return result;
}
function disableForm(formId,isDisabled) {
var attr="disable";
if(!isDisabled){
attr="enable";
}
$("form[id='"+formId+"'] :text").attr("disabled",isDisabled);
$("form[id='"+formId+"'] textarea").attr("disabled",isDisabled);
$("form[id='"+formId+"'] select").attr("disabled",isDisabled);
$("form[id='"+formId+"'] :radio").attr("disabled",isDisabled);
$("form[id='"+formId+"'] :checkbox").attr("disabled",isDisabled);
//禁用jquery easyui中的下拉选(使用input生成的combox)
$("#" + formId + " input[class='easyui-combobox']").each(function () {
if (this.id) {
$("#" + this.id).combobox({disabled:isDisabled});
}
});
//禁用jquery easyui中的下拉选(使用select生成的combox)
$("#" + formId + " select[class='easyui-combobox']").each(function () {
if (this.id) {
$("#" + this.id).combobox({disabled:isDisabled});
}
});
//禁用jquery easyui中的日期组件dataBox
$("#" + formId + " input[class='easyui-datebox']").each(function () {
if (this.id) {
$("#" + this.id).datebox({disabled:isDisabled});
}
});
//禁用jquery easyui中的日期组件dataBox
$("#" + formId + " input[class='easyui-datetimebox']").each(function () {
if (this.id) {
$("#" + this.id).datetimebox({disabled:isDisabled});
}
});
$("#"+formId+" .easyui-combobox").combobox({ disabled: isDisabled});
$("#"+formId+" .easyui-datebox").datebox({ disabled: isDisabled});
$("#"+formId+" .easyui-datetimebox").datetimebox({ disabled: isDisabled});
}
function compareTime(startDate, endDate) {
if (startDate.length > 0 && endDate.length > 0) {
var startDateTemp = startDate.split(" ");
var endDateTemp = endDate.split(" ");
var arrStartDate = startDateTemp[0].split("-");
var arrEndDate = endDateTemp[0].split("-");
var arrStartTime = startDateTemp[1].split(":");
var arrEndTime = endDateTemp[1].split(":");
var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]);
var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]);
if (allStartDate.getTime() >= allEndDate.getTime()) {
return false;
} else {
return true;
}
} else {
return false;
}
}
function getContextPath() {
var host = $.url.attr('host');
var protocol = $.url.attr('protocol');
var port = $.url.attr('port');
var path = $.url.attr('path');
var contextPath;
if (port == null) {
contextPath = protocol + '://' + host + '/' + path.split('/')[1];
} else {
contextPath = protocol + '://' + host + ':' + port + '/' + path.split('/')[1];
}
return contextPath;
}
function loading()
{
$.blockUI({ css: {
border: 'none',
padding: '10px',
backgroundColor: '#000',
'-webkit-border-radius': '10px',
'-moz-border-radius': '10px',
'z-index':12000,
opacity: .5,
color: '#fff'
} });
}
function loaded(){
$.unblockUI();
}
function alertMsg(title,msg){
$.messager.alert(title, msg, 'info');
}