通用js类库


/* 其它通用函数 */
$(function() {

//
var General = function() {
var _self = this;

/* 写 cookie 操作 */
_self.SetCookie = function(c_name, value, expireTime, dhm) {
var exdate = new Date();
if (dhm == "d") {
exdate.setDate(exdate.getDate() + parseInt(expireTime, 10));
}
else if (dhm == "h") {
exdate.setHours(exdate.getHours() + parseInt(expireTime, 10));
}
else if (dhm == "m") {
exdate.setMinutes(exdate.getMinutes() + parseInt(expireTime, 10));
}
document.cookie = c_name + "=" + escape(value) + ((expireTime == null) ? "" : ";expires=" + exdate.toGMTString());
};

/* 读 cookie 操作 */
_self.GetCookie = function(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=")
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return null
};

/* 删除 cookie 操作 */
_self.DeleteCookie = function(c_name) {
var exdate = new Date();
exdate.setDate(exdate.getDate() - parseInt(3650, 10));
document.cookie = c_name + "=;expires=" + exdate.toGMTString();
};

/* 生成GUID */
_self.GUID = function() {
// 8 4 4 4 12
var times = [8, 4, 4, 4, 12];
var baseStr = "abcdefg56789hijklm12389nopqrst45670uvwxyz12340";
var gui = new Array();
for (var i = 0; i < times.length; i++) {
var guid = new Array();
for (var j = 0; j < times[i]; j++) {
guid.push(baseStr.substr(_self.RandomNumber(baseStr.length), 1));
}
gui.push(guid.join(""));
}
return gui.join("-");
};

/* 生成随机数 */
_self.RandomNumber = function(range) {
return parseInt(Math.floor(Math.random() * range), 10);
};

/* 字符串转换成日期 */
_self.ConvertToDate = function(str) {
var y = parseInt(str.split('-')[0], 10);
var m = parseInt(str.split('-')[1], 10) - 1;
var d = parseInt(str.split('-')[2].split(' ')[0], 10);
var h = parseInt(str.split('-')[2].split(' ')[1].split(':')[0], 10);
var mm = parseInt(str.split('-')[2].split(' ')[1].split(':')[1], 10);
var s = parseInt(str.split('-')[2].split(' ')[1].split(':')[2], 10);
var dt = new Date(y, m, d, h, mm, s, 0);
return dt;
};

/* 日期转换成字符串 */
_self.ConvertDateToString = function(dt) {
var y = dt.getFullYear();
var m = "0" + (dt.getMonth() + 1);
var d = "0" + dt.getDate();
var h = "0" + dt.getHours();
var mm = "0" + dt.getMinutes();
var s = "0" + dt.getSeconds();

m = m.substr(m.length - 2, 2);
d = d.substr(d.length - 2, 2);
h = h.substr(h.length - 2, 2);
mm = mm.substr(mm.length - 2, 2);
s = s.substr(s.length - 2, 2);

return y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
};

/* 样式表样式 */
/* 在Firefox中,无法直接取得样式表样式 */
_self.Style = function(element, style, isColor) {
if (!$.browser.msie) {
// FF等支持
if (isColor) {
return document.defaultView.getComputedStyle(element, null).getPropertyValue(style).toHEX();
}
else {
return document.defaultView.getComputedStyle(element, null).getPropertyValue(style);
}
}
else {
//IE支持
if (isColor) {
style = style.replace("-top-", "-").replace("-right-", "-").replace("-bottom-", "-").replace("-left-", "-").replace("-color", "Color");
}
return element.currentStyle[style];
}
};

/* 判断是否为函数 */
_self.IsFunction = function(funcName) {
return typeof (eval(funcName)) == "function";
};

/* 输出AJAX返回XML错误结果 */
_self.AJAXMLErr = function(data) {
if ($(data).find("etdz>err").attr("data").indexOf("重新登录") != -1) {
window.location.href = "logout.aspx?x=" + Math.random();
}
};

/* 判断是否为有效日期 */
_self.ValidDate = function(dateStr) {
try {
var yy = dateStr.split('-')[0];
var mm = dateStr.split('-')[1];
var dd = dateStr.split('-')[2];
var a = new Date(parseInt(yy, 10), parseInt(mm, 10) - 1, parseInt(dd, 10), 0, 0, 0, 0);
var y = a.getFullYear();
var m = a.getMonth() + 1;
var d = a.getDate();
var myday = y + "-" + ("0" + m.toString()).substr(("0" + m.toString()).length - 2) + "-" + ("0" + d.toString()).substr(("0" + d.toString()).length - 2);
dateStr = yy + "-" + ("0" + mm).substr(("0" + mm).length - 2) + "-" + ("0" + dd).substr(("0" + dd).length - 2);
if (myday != dateStr) {
return false;
}
return true;
}
catch (Error) {
return false;
}
};

/* Request.QueryString */
_self.Request = {
QueryString: function(item) {
var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
return svalue ? svalue[1] : svalue;
}
}

_self.ReLogin = function() {
_self.Trans();
var _un = _self.GetCookie("username") == null ? "" : _self.GetCookie("username").toString();
var rlHtml = "<p style=\"padding-bottom:10px;border-bottom:solid 1px #ccc;margin-bottom:20px;\" class=\"cRed\">身份失效,请重新登录!</p><p> 用户名:<input type=\"text\" class=\"rlTxt\" id=\"rlUsername\" value=\"" + _un + "\" /></p><p>  密码:<input type=\"password\" class=\"rlTxt\" id=\"rlPswd\" /></p><p>     <button type=\"button\" id=\"btnReLogin\">登录</button> <input type=\"checkbox\" id=\"rlAuto\"/><label for=\"rlAuto\">自动登录</label></p><p style=\"padding-top:5px;\">     <span id=\"reLoginInfo\"></span></p>";
$("<div/>", { id: "innerLoginShadow", css: { top: ((document.documentElement.clientHeight - 300) / 2 - 8) + "px", left: ((document.documentElement.clientWidth - 280) / 2 - 8) + "px"} }).appendTo($("#_topTransLayer"));
var rl = $("<div/>", { id: "innerLogin", css: { top: ((document.documentElement.clientHeight - 300) / 2) + "px", left: ((document.documentElement.clientWidth - 280) / 2) + "px"} }).html(rlHtml).appendTo($("#_topTransLayer"));

if ($("#rlUsername").val() == "") {
$("#rlUsername").trigger("focus").trigger("select");
}
else {
$("#rlPswd").trigger("focus").trigger("select");
}

$("#btnReLogin").click(function() {

/* validation */
var un = $("#rlUsername").val().trim().clearHtmlTag();
if (un == "") {
$("#reLoginInfo").css({ color: "#f00" }).html("请填写用户名!");
$("#rlUsername").trigger("focus").trigger("select");
return;
}
if (!un.matchRegex(/^[0-9a-zA-Z]{1}[0-9a-zA-Z\@\_\.\-]{1,28}[0-9a-zA-Z]{1}$/)) {
$("#reLoginInfo").css({ color: "#f00" }).html("用户名格式错误!");
$("#rlUsername").trigger("focus").trigger("select");
return;
}
var pw = $("#rlPswd").val().trim().clearHtmlTag();
if (pw == "") {
$("#reLoginInfo").css({ color: "#f00" }).html("请填写密码!");
$("#rlPswd").trigger("focus").trigger("select");
return;
}
var autologin = $("#rlAuto").attr("checked");

$("#btnReLogin").hide();
$("#rlUsername").attr("disabled", true);
$("#rlPswd").attr("disabled", true);
$("#rlAuto").attr("disabled", true);
loginng = true;
var rli = 0;
(function() {
if (loginng) {
rli++;
if (rli % 2 == 0) {
$("#reLoginInfo").css({ color: "#000" }).html(" ");
}
else {
$("#reLoginInfo").css({ color: "#000" }).html("正在登录,请稍候");
}
setTimeout(arguments.callee, 500);
}
})();

/* login */
$.post("xmls/index_login.aspx?x=" + Math.random(), {
"un": un,
"pw": pw,
"auto": autologin,
"fromUrl": ""
}, function(data, textStatus) {
loginng = false;
rli = 0;
if ($(data).find("etdz>err").attr("data") == "null") {
$("#reLoginInfo").css({ color: "#690" }).html("登录成功!");
setTimeout(function() {
_self.HideTrans();
}, 500);
}
else {
$("#btnReLogin").show();
$("#rlUsername").attr("disabled", false);
$("#rlPswd").attr("disabled", false);
$("#rlAuto").attr("disabled", false);
var err = $(data).find("etdz>err").attr("data");
$("#reLoginInfo").css({ color: "#f00" }).html(err);
if (err.indexOf("用户名") != -1) {
$("#rlUsername").trigger("focus").trigger("select");
}
else if (err.indexOf("密码") != -1) {
$("#rlPswd").trigger("focus").trigger("select");
}
}
});
});
};

_self.Trans = function() {
var tl = $("<div/>", { id: "_topTransLayer", css: { position: "absolute", width: "10000px", height: "10000px", top: "0px", left: "0px", background: "url(img/blank.gif) repeat top left"} }).appendTo($("body"));
$("html").css({ "overflow-x": "hidden" });
return tl;
};

_self.HideTrans = function() {
$("#_topTransLayer").remove();
$("html").css({ "overflow-x": "auto" });
};

/* table info */
_self.TableInfo = function(listName) {
return $("#" + listName).parent().parent().find("span[class='tableName']:eq(1)");
};

_self.Alarm = function(msg, infoContainer) {
if (msg.indexOf('身份已失效,请重新登录!') != -1) {
_self.ReLogin();
}
else {
if (infoContainer == null) {
alert(msg);
}
else {
if ($(infoContainer) != null) {
$(infoContainer).html(msg);
}
}
}
};

_self.SetPager = function(allPages, _func) {
allPages = parseInt(allPages, 10);
if (allPages == 999999) {
$(".pageSummary").html("第" + _page + "页 ");
}
else {
$(".pageSummary").html("第" + _page + "/" + allPages + "页 ");
}
$(".firstPage").unbind("click").click(function() {
_page = 1;
_func();
});
$(".nextPage").unbind("click").click(function() {
if (_page + 1 > allPages) {
return;
}
_page++;
_func();
});
$(".prevPage").unbind("click").click(function() {
if (_page - 1 < 1) {
return;
}
_page--;
_func();
});
};

/* 其它 */

};

/* 通用函数实例 */
_G = new General();

/* 内页通用设置 */
if ($("script[src*='js/index.js']").length == 0) {

// form tabs
$("#tabs>span").click(function() {
$(".forms").hide();
$("#" + $(this).attr("target")).show();
$("#tabs>span").removeClass("on");
$(this).addClass("on");
});

// nav submenu
$("#top_top>a").mouseover(function() {
_onMenu = true;
$(document).unbind("click");
$("#submenu").hide().html("").css({ left: $(this).offset().left + "px" });
if ($(this).attr("submenu") == undefined) {
return;
}
var sm = $(this).attr("submenu").split(',');
for (var i = 0; i < sm.length; i++) {
$("<span/>").html("- " + sm[i].split('=')[0]).attr("href", sm[i].split('=')[1]).appendTo($("#submenu"));
}
$("#submenu").show();
$(document).click(function() {
$("#submenu").hide().html("");
_onMenu = false;
});
$("#submenu").unbind("mouseover").mouseover(function() {
$(document).unbind("click");
_onMenu = true;
}).unbind("mouseout").mouseout(function() {
$(document).unbind("click").click(function() {
$("#submenu").hide().html("");
_onMenu = false;
});
_onMenu = false;
setTimeout(function() {
if (!_onMenu) {
$(document).unbind("click");
$("#submenu").hide().html("");
_onMenu = false;
}
}, 5000);
});
$("#submenu>span").click(function() {
window.location.href = $(this).attr("href") + "?x=" + Math.random();
});
}).click(function() {
if ($(this).attr("submenu") != undefined) {
window.location.href = $(this).attr("submenu").split(',')[0].split('=')[1] + "?x=" + Math.random();
}
});

$("#frmMain").submit(function() {
return false;
});
}
});

var _G = null;
var _page = 1;
var _onMenu = false;
// *******************************************************
// *******************************************************
// *******************************************************

// the following should be change to false in runtime

var _DEBUG = true;

// *******************************************************
// *******************************************************
// *******************************************************

function ImagePreload(img) {
for (var i = 0; i < img.length; i++) {
var _img = img[i].split(',');
var im = new Image(parseInt(_img[0], 10), parseInt(_img[1], 10));
im.src = _img[2];
}
}

/* 清除字符串两边空格 */
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}

/* 清除字符串中的html字符 */
String.prototype.clearHtmlTag = function() {
return this.replace(/\>/g, '>').replace(/\</g, '<').replace(/\&/g, '&').replace(/\'/g, "''");
}

/* 匹配正则表达式 */
String.prototype.matchRegex = function(parten) {
if (parten.exec(this)) {
return true;
}
else {
return false;
}
}

/* 颜色转换10进制-16进制 */
// '0,255,255' > '#00ffff'
String.prototype.toHEX = function() {
var color10 = this;
if (color10.indexOf("#") != -1) {
return color10;
}
// if (color10.indexOf("rgb(") == 0) {
// color10 = color10.replace('rgb(', '').replace(')', '').replace(/\s/g, '');
// }
var r = parseInt(color10.split(',')[0], 10).toString(16);
var g = parseInt(color10.split(',')[1], 10).toString(16);
var b = parseInt(color10.split(',')[2], 10).toString(16);
r = ("0" + r).substr(("0" + r).length - 2);
g = ("0" + g).substr(("0" + g).length - 2);
b = ("0" + b).substr(("0" + b).length - 2);
return "#" + r + g + b;
}

/* 颜色转换16进制-10进制 */
// '#00ffcc' > Array()[r,g,b]
String.prototype.toDEC = function() {
var color16 = this;
if (!color16.matchRegex(/^\#[0-9a-zA-Z]{1,2}[0-9a-zA-Z]{1,2}[0-9a-zA-Z]{1,2}$/)) {
return color16;
}
if (color16.indexOf("#") == 0) {
color16 = color16.replace('#', '');
}
var r = parseInt(color16.substr(0, 2), 16);
var g = parseInt(color16.substr(2, 2), 16);
var b = parseInt(color16.substr(4, 2), 16);
return [r, g, b];
}

/* 字符串转XML对象 */
String.prototype.toXML = function() {
var xmlStr = this;
if ($.browser.msie) { // for ie
var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
xmlDom.loadXML(xmlStr);
return $(xmlDom);
}
else { // for non-ie
var xmlDom = new DOMParser().parseFromString(xmlStr, "text/xml");
return $(xmlDom);
}
}

/* 字符串为空则转为&nbsp; */
String.prototype.toNBSP = function() {
var str = this;
if (str.length == 0) {
return "&nbsp;";
}
else {
return str;
}
}

/* 格式化金额 */
String.prototype.toPrice = function() {
var price = this;
price = parseFloat(price);
price = price.toFixed(2);
if (price.toString().indexOf(".") != -1) {
return price.toString().split('.')[0] + "." + (price.toString().split('.')[1] + "0").substr(0, 2);
}
else {
return price.toString() + ".00";
}
}

/* 反转字符串 */
String.prototype.reverse = function() {
var str = this;
var st = str.toString();
var newStr = new Array();
for (var i = 0; i < st.length; i++) {
newStr.push(st.substr(i,1));
}
st = newStr.reverse().join("");
newStr = null;
return st;
}

/* 判断某字符在字符串中出现的次数 */
String.prototype.timesOf = function(s) {
var str = this;
return str.split(s).length - 1;
}
var _sat = 100;
var _bri = 60;

posted @ 2013-08-29 14:19  无处安放的青春  阅读(409)  评论(0编辑  收藏  举报