国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 互联网 > 网站常用的一些javascript封装 简化调用

网站常用的一些javascript封装 简化调用

来源:程序员人生   发布时间:2014-11-12 09:09:55 阅读次数:2229次
//用于网页地址参数 //参数中包括出了英文中文数字以外的其他符号时进行编码并在前面加“==”进行标识,否则直接返回 //解码时根据是不是含有“==”标识来决定是不是要解码 var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var base64Encode = new Array( ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, 62, ⑴, ⑴, ⑴, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ⑴, ⑴, ⑴, ⑴, ⑴, ⑴, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, ⑴, ⑴, ⑴, ⑴, ⑴); function Base64Encode(str) { var reg=/^[a-zA-Z0⑼]*$/; if(str==null || reg.test(str)) { return str; } str=Utf16To8(str); var out, i, len; var c1, c2, c3; len = str.length; i = 0; out = ""; while(i < len) { c1 = str.charCodeAt(i++) & 0xff; if(i == len) { out += base64EncodeChars.charAt(c1 >> 2); out += base64EncodeChars.charAt((c1 & 0x3) << 4); out += "=="; break; } c2 = str.charCodeAt(i++); if(i == len) { out += base64EncodeChars.charAt(c1 >> 2); out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xf0) >> 4)); out += base64EncodeChars.charAt((c2 & 0xf) << 2); out += "="; break; } c3 = str.charCodeAt(i++); out += base64EncodeChars.charAt(c1 >> 2); out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xf0) >> 4)); out += base64EncodeChars.charAt(((c2 & 0xf) << 2) | ((c3 & 0xc0) >>6)); out += base64EncodeChars.charAt(c3 & 0x3f); } out=out.replace(///g,"@@") return "==" + out; } function Utf16To8(str) { var out, i, len, c; out = ""; len = str.length; for (i = 0; i < len; i++) { c = str.charCodeAt(i); if ((c >= 0x0001) && (c <= 0x007f)) { out += str.charAt(i); } else if (c > 0x07ff) { out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f)); out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f)); } else { out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f)); } } return out; } function Utf8To16(str) { var out, i, len, c; var char2, char3; out = ""; len = str.length; i = 0; while (i < len) { c = str.charCodeAt(i++); switch (c >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: // 0xxxxxxx out += str.charAt(i - 1); break; case 12: case 13: // 110x xxxx 10xx xxxx char2 = str.charCodeAt(i++); out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f)); break; case 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charCodeAt(i++); char3 = str.charCodeAt(i++); out += String.fromCharCode(((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)); break; } } return out; } //base64编码结束 function ValidateEmail(str) { var r = /^[a-zA-Z0⑼._%-]+@[a-zA-Z0⑼.-]+.[a-zA-Z]{2,4}$/; if (r.test(str)) { return true; } else { return false; } } function ValidateTelPhone(str) { var s = /^(d{2,4})(-(d{7,8}))$/; if (s.test(str)) { return true; } else { return false; } } function ValidatePhone(str) { var regu = /^[1][3,5,8][0⑼]{9}$/; var re = new RegExp(regu); if (re.test(str)) { return true; } else { return false; } } /*设置与获得Cookie*/ var Cookie = {} Cookie.write = function (key, value, duration) { var d = new Date(); d.setTime(d.getTime() + 1000 * 60 * 60 * 24 * 30); document.cookie = key + "=" + encodeURI(value) + "; expires=" + d.toGMTString(); }; Cookie.read = function (key) { var arr = document.cookie.match(new RegExp("(^| )" + key + "=([^;]*)(;|$)")); if (arr != null) return decodeURIComponent(arr[2]); return ""; }; //input去掉默许内容还原编辑样式 function g_on_setvalue(id, value, cname) { var obj = document.getElementById(id); if (obj.value != value) { return false; } obj.value = ""; obj.className = cname; } //将日期转换成"yyyy-mm-dd"格式 第2个参数为返回类型传入'ym':yyyy-mm,'md':mm-dd,默许为'yyyy-mm-dd' function ConvertDateFormat(strDate, sye) { if (strDate == null || strDate == "" || strDate == "0") { return (""); } else { try { var ExDate = new Date(strDate.replace(/-/g, "/")); var yyyy = ExDate.getFullYear(); var mm = ExDate.getMonth() + 1; mm = mm < 10 ? "0" + mm : mm; var dd = ExDate.getDate(); dd = dd < 10 ? "0" + dd : dd; switch (sye) { case "ym": return yyyy + "-" + mm; break; case "md": return mm + "-" + dd; break; default: return yyyy + "-" + mm + "-" + dd; break; } } catch (e) { return ("") } } } //将日期转换成"yyyymmdd"格式 第2个参数为返回类型传入'ym':yyyy-mm,'md':mm-dd,默许为'yyyy-mm-dd' function ConvertDateFormatString(strDate, sye) { if (strDate == null || strDate == "" || strDate == "0" || strDate.length < 8) { return (""); } else { try { var ExDate = new Date(strDate.replace(/^(d{4})(d{2})(d{2})$/, "$1/$2/$3")); var yyyy = ExDate.getFullYear(); var mm = ExDate.getMonth() + 1; mm = mm < 10 ? "0" + mm : mm; var dd = ExDate.getDate(); dd = dd < 10 ? "0" + dd : dd; switch (sye) { case "ym": return yyyy + "-" + mm; break; case "md": return mm + "-" + dd; break; default: return yyyy + "-" + mm + "-" + dd; break; } } catch (e) { return ("") } } } //获得URL中的request参数 function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) { return decodeURIComponent(r[2]); } else { return ""; } } function aUrl(url) { window.location.href = url; } function rForm(formid) { document.getElementById(formid).reset(); } //全选 function checkAll(id, checkname) { var obj = document.getElementById(id); if (obj.checked) { $("input[name='" + checkname + "']").attr('checked', true) } else { $("input[name='" + checkname + "']").attr('checked', false) } } //取checkbox 返回数组 function checkvalue(name) { var obj = document.getElementsByName(name); var aList = []; var j = 0; for (var i = 1; i < obj.length; i++) { if (obj[i].checked) { j = j + 1; aList[j - 1] = obj[i].value; } } if (aList.length == 0) { alert("最少选择1条记录"); return false; } else { return aList; } } //html编码 function HTMLEncode(input) { var converter = document.createElement("DIV"); converter.innerText = input; var output = converter.innerHTML; converter = null; return output; } //html编码解码 function HTMLDecode(input) { var converter = document.createElement("DIV"); converter.innerHTML = input; var output = converter.innerText; converter = null; return output; } //tab标签层切换 function setTabDiv(curnum, checkstatus) { if (checkstatus) { $("#tabtooldiv a").removeClass("curtabbar"); $("#tabelement" + curnum).addClass("curtabbar"); $("div[name='tabconent']").css("display", "none"); $("#tabconent" + curnum).css("display", "block"); } } //验证金额 function checkPrice(thisobject) { if ($(thisobject).val().toString() == "") {return;} if ((/^-?(([1⑼]d*)|d)(.d{1,4})?$/).test($(thisobject).val().toString())) { } else { alert("输入金额格式毛病请重新输入!"); $(thisobject).val(""); } } //限制文本输入长度 function isMaxLen(o) { var nMaxLen = o.getAttribute ? parseInt(o.getAttribute("maxlength")) : ""; if (o.getAttribute && o.value.length > nMaxLen) { o.value = o.value.substring(0, nMaxLen) } } //验证整数 function intcheck(thisobject) { if ($(thisobject).val().toString() == "") { return; } if ($(thisobject).val().toString() == "0") { return; } var number = $(thisobject).val(); var type = "^[0⑼]*[1⑼][0⑼]*$"; var re = new RegExp(type); if (number.match(re) == null) { alert("请输入大于零的整数!"); $(thisobject).val(""); return; } } //比较时间 function comTime(begintime, endtime) { var arr = begintime.split("-"); var starttime = new Date(arr[0], arr[1], arr[2]); var starttimes = starttime.getTime(); var arrs = endtime.split("-"); var lktime = new Date(arrs[0], arrs[1], arrs[2]); var lktimes = lktime.getTime(); if (starttimes >= lktimes) { return false; } else return true; } //open window function showMyModalDialog(url, width, height,Title) { var now = new Date(); var urlr if (url.indexOf('=') == ⑴) { urlr = url + "?cach=" + escape(now); } else { urlr = url + "&cach=" + escape(now); } if (Title !=null) { JqueryDialog.Open(Title, urlr, width, height); } else { JqueryDialog.Open('', urlr, width, height); } } //open mini window function openwindow(url, height, width) { var t = (window.screen.availHeight - 400) / 2; var l = (window.screen.availWidth - 600) / 2; window.open(url, "Query", "height=" + height + ", width=" + width + ", top=" + t + ",left=" + l + ",toolbar=no, menubar=no, scrollbars=yes, resizable=no"); } //confirm function Confirm(msg, control) { $.messager.confirm('确认', msg, function (r) { if (r) { eval(control.toString().slice(11)); } }); return false; } //gridview function gridview(objgridview) { //get obj id var gridviewId = "#" + objgridview; //even $(gridviewId + ">tbody tr:even").addClass("NormalColor"); //first $(gridviewId + ">tbody tr:first").removeClass("NormalColor").addClass("HeadColor"); //odd $(gridviewId + ">tbody tr:odd").addClass("AlterColor"); //move and click edit by hb 20130206 $(gridviewId + ">tbody tr").hover(function () { $(this).addClass("HoverColor"); }, function () { $(this).removeClass("HoverColor"); }).click(function () { var $check = $(this).find("input:checkbox"); if ($check.attr("checked")) { $(this).addClass("SelectColor"); } else { $(this).removeClass("SelectColor"); } }); //all check $("#chkAll").click(function () { $(gridviewId + '>tbody >tr >td >input:checkbox:visible').attr('checked', this.checked); }); //check status $(gridviewId + ' >tbody >tr >td >input:checkbox').click(function () { var expr1 = gridviewId + ' >tbody >tr >td >input:checkbox:checked'; var expr2 = gridviewId + ' >tbody >tr >td >input:checkbox'; var selectAll = $(expr1).length == $(expr2).length; $('#chkAll').attr('checked', selectAll); }); } //load function Load() { $("<div class="datagrid-mask"></div>").css({ display: "block", width: "100%", height: $(window).height() }).appendTo("body"); $("<div class="datagrid-mask-msg"></div>").html("正在运行,请稍候。。。").appendTo("body").css({ display: "block", left: ($(document.body).outerWidth(true) - 190) / 2, top: ($(window).height() - 45) / 2 }); } //display Load function dispalyLoad() { $(".datagrid-mask").remove(); $(".datagrid-mask-msg").remove(); } function checkroleinfo(checkresult, checkaction) { if (checkresult == "1") { } else { $("[rolecheck='" + checkaction + "']").remove(); } } function showLoad(tipInfo) { var eTip = document.createElement('div'); eTip.setAttribute('id', 'tipDiv'); eTip.style.position = 'absolute'; eTip.style.display = 'none'; eTip.style.border = 'solid 0px #D1D1D1'; eTip.style.backgroundColor = '#4B981D'; eTip.style.padding = '5px 15px'; eTip.style.top = '10px'; eTip.style.right = '10px'; eTip.style.width = '120px'; eTip.innerHTML = '<img src='/Images/loader.gif' style='float:left;' />  <span style='color:#ffffff; font-size:12px'>' + tipInfo + '</span>'; try { document.body.appendChild(eTip); } catch (e) { } $("#tipDiv").css("float", "right"); $("#tipDiv").css("z-index", "99"); $('#tipDiv').fadeIn(); } function closeLoad() { $('#tipDiv').fadeOut(); } function ForDight(Dight, How) { Dight = Math.round(Dight * Math.pow(10, How)) / Math.pow(10, How); return Dight; } function changetitle(object) { $(object).attr("title", $(object).val()); } function atoc(numberValue) { var numberValue = new String(Math.round(numberValue * 100)); // 数字金额 var chineseValue = ""; // 转换后的汉字金额 var String1 = "零壹贰叁肆伍陆柒捌玖"; // 汉字数字 var String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; // 对应单位 var len = numberValue.length; // numberValue 的字符串长度 var Ch1; // 数字的汉语读法 var Ch2; // 数字位的汉字读法 var nZero = 0; // 用来计算连续的零值的个数 var String3; // 指定位置的数值 if (len > 15) { alert("超越计算范围"); return ""; } if (numberValue == 0) { chineseValue = "零元整"; return chineseValue; } String2 = String2.substr(String2.length - len, len); // 取出对应位数的STRING2的值 for (var i = 0; i < len; i++) { String3 = parseInt(numberValue.substr(i, 1), 10); // 取出需转换的某1位的值 if (i != (len - 3) && i != (len - 7) && i != (len - 11) && i != (len - 15)) { if (String3 == 0) { Ch1 = ""; Ch2 = ""; nZero = nZero + 1; } else if (String3 != 0 && nZero != 0) { Ch1 = "零" + String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else { Ch1 = String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } } else { // 该位是万亿,亿,万,元位等关键位 if (String3 != 0 && nZero != 0) { Ch1 = "零" + String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else if (String3 != 0 && nZero == 0) { Ch1 = String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else if (String3 == 0 && nZero >= 3) { Ch1 = ""; Ch2 = ""; nZero = nZero + 1; } else { Ch1 = ""; Ch2 = String2.substr(i, 1); nZero = nZero + 1; } if (i == (len - 11) || i == (len - 3)) { // 如果该位是亿位或元位,则必须写上 Ch2 = String2.substr(i, 1); } } chineseValue = chineseValue + Ch1 + Ch2; } if (String3 == 0) { // 最后1位(分)为0时,加上“整” chineseValue = chineseValue + "整"; } return chineseValue; } var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; function generateMixed(n) { var res = ""; for (var i = 0; i < n; i++) { var id = Math.ceil(Math.random() * 35); res += chars[id]; } return res; } /// <summary> /// 实现数据的4舍5入法 /// </summary> /// <param name="v">要进行处理的数据</param> /// <param name="x">保存的小数位数</param> /// <returns>4舍5入后的结果</returns> function Round(v, x) { var isNegative = false; //如果是负数 if (v < 0) { isNegative = true; v = -v; } var IValue = 1; for (var i = 1; i <= x; i++) { IValue = IValue * 10; } var Int = Math.round(v * IValue, 0); v = Int / IValue; if (isNegative) { v = -v; } return v; } /// <summary> /// 实现数据的向上取整 /// </summary> /// <param name="v">要进行处理的数据</param> /// <param name="x">保存的小数位数</param> /// <returns>向上取整后的结果</returns> function Ceiling(v, x) { var isNegative = false; //如果是负数 if (v < 0) { isNegative = true; v = -v; } var IValue = 1; for (var i = 1; i <= x; i++) { IValue = IValue * 10; } var Int = Math.ceil(v * IValue); v = Int / IValue; if (isNegative) { v = -v; } return v; } /// <summary> /// 实现数据的向下取整 /// </summary> /// <param name="v">要进行处理的数据</param> /// <param name="x">保存的小数位数</param> /// <returns>向下取整后的结果</returns> function Floor(v, x) { var isNegative = false; //如果是负数 if (v < 0) { isNegative = true; v = -v; } var IValue = 1; for (var i = 1; i <= x; i++) { IValue = IValue * 10; } var Int = Math.floor(v * IValue); v = Int / IValue; if (isNegative) { v = -v; } return v; } /// <summary> /// 实现数据的4舍5入,向上取整,向下取整 /// </summary> /// <param name="v">要进行处理的数据</param> /// <param name="x">保存的小数位数</param> /// <param name="type">14舍5入,2向上取整,3向下取整</param> /// <returns></returns> function DecimalProcessing(v, x, type) { var result = 0; switch (type) { case "1": result = Round(v, x); break; case "2": result = Ceiling(v, x); break; case "3": result = Floor(v, x); break; default: result = Round(v, x); break; } return result; } //除法函数,用来得到精确的除法结果 // 说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。 //调用:accDiv(arg1,arg2) //返回值:arg1除以arg2的精确结果 function accDiv(arg1, arg2) { var t1 = 0, t2 = 0, r1, r2; try { t1 = arg1.toString().split(".")[1].length } catch (e) { } try { t2 = arg2.toString().split(".")[1].length } catch (e) { } with (Math) { r1 = Number(arg1.toString().replace(".", "")) r2 = Number(arg2.toString().replace(".", "")) return (r1 / r2) * pow(10, t2 - t1); } } //给Number类型增加1个div方法,调用起来更加方便。 Number.prototype.div = function (arg) { return accDiv(this, arg); } //乘法函数,用来得到精确的乘法结果 //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。 //调用:accMul(arg1,arg2) //返回值:arg1乘以 arg2的精确结果 function accMul(arg1, arg2) { var m = 0, s1 = arg1.toString(), s2 = arg2.toString(); try { m += s1.split(".")[1].length } catch (e) { } try { m += s2.split(".")[1].length } catch (e) { } return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m) } // 给Number类型增加1个mul方法,调用起来更加方便。 Number.prototype.mul = function (arg) { return accMul(arg, this); } //加法函数,用来得到精确的加法结果 //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。 //调用:accAdd(arg1,arg2) // 返回值:arg1加上arg2的精确结果 function accAdd(arg1, arg2) { var r1, r2, m, c; try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 } try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } c = Math.abs(r1 - r2); m = Math.pow(10, Math.max(r1, r2)) if (c > 0) { var cm = Math.pow(10, c); if (r1 > r2) { arg1 = Number(arg1.toString().replace(".", "")); arg2 = Number(arg2.toString().replace(".", "")) * cm; } else { arg1 = Number(arg1.toString().replace(".", "")) * cm; arg2 = Number(arg2.toString().replace(".", "")); } } else { arg1 = Number(arg1.toString().replace(".", "")); arg2 = Number(arg2.toString().replace(".", "")); } return (arg1 + arg2) / m } //给Number类型增加1个add方法,调用起来更加方便。 Number.prototype.add = function (arg) { return accAdd(arg, this); } //减法函数,用来得到精确的减法结果 //说明:javascript的减法结果会有误差,在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果。 //调用:accSub(arg1,arg2) // 返回值:arg1加上arg2的精确结果 function accSub(arg1, arg2) { var r1, r2, m, n; try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 } try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度 n = (r1 >= r2) ? r1 : r2; return ((arg1 * m - arg2 * m) / m).toFixed(n); }

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生