博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
记录JavaScript的util.js类库
阅读量:4931 次
发布时间:2019-06-11

本文共 8831 字,大约阅读时间需要 29 分钟。

工作中用到的, 不断做为积累, 以后能用到. 也感谢前辈们. 

定义Util对象

var MyUtil = new Object();

从url中获取参数

//从url中获取参数function GetQueryString(name){    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");    var r = window.location.search.substr(1).match(reg);    if(name == 'qu_name'){        if(r!=null)return  decodeURI(r[2]); return null;    }else{        if(r!=null)return  unescape(r[2]); return null;    }}

生成32位随机数

// 生成32位随机数          function randomString() {    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';        var maxPos = $chars.length;    var pwd = '';    for (var i = 0; i < 32; i++) {        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));    }    return pwd;}

隐藏DIV

MyUtil.hideDiv = function(div) {    div.css("display", "none");};

弹出div显示层

/** * 弹出div显示层 *  * @param {} *            div 需要显示的div元素 * @param {} *            content div中显示的内容 * @param {} *            left 左偏移量 * @param {} *            top 顶部偏移量 * @param {} *            width div宽度 * @param {} *            height div高度 */MyUtil.showDiv = function(div, content, left, top, width, height) {    div.empty();    div.append(content);    HuaDun.setPosition(div, left, top);    if (typeof width != 'undefined' && (width != null || width != "")) {        div.css("width", width + "px");    }    if (typeof height != 'undefined' && (height != null || height != "")) {        div.css("height", height + "px");    }    div.css("display", "block");};

设置弹出div位置

/** * 设置弹出div位置 *  * @param {} *            div 需要显示的div元素 * @param {} *            x 左偏移量 * @param {} *            y 顶部偏移量 */MyUtil.setPosition = function(div, x, y) {    var top = document.body.scrollTop || document.documentElement.scrollTop;    var left = document.body.scrollLeft || document.documentElement.scrollLeft;    x += left;    y += top;    var l = x + 20;// 左偏移量    var t = y - (div.height() - 20); // 顶部偏移量    var bRight = true;    var iPageRight = left + document.documentElement.clientWidth;// 页面的右边界    if (l + div.width() > iPageRight) {        bRight = false;        l = x - (div.width() + 20);    }    div.css("left", l + 'px');    div.css("top", t + 'px');}

时间工具类

/** * 时间类工具 */var MyDate= new Object();// 获取当前时间MyDate.GetNow = function() {    var now = new Date();    var year = now.getFullYear();    var month = now.getMonth() + 1;    var day = now.getDate();    var hh = now.getHours(); // 获取当前小时数(0-23)    var mm = now.getMinutes(); // 获取当前分钟数(0-59)    var ss = now.getSeconds();    var clock = year + "-";    if (month < 10)        clock += "0";    clock += month + "-";    if (day < 10)        clock += "0";    clock += day;    var h = parseInt(hh);    if (h < 10) {        h = "0" + h;    }    clock += " " + h;    var m = parseInt(mm);    if (m < 10) {        m = "0" + m;    }    clock += ":" + m;    var s = parseInt(ss);    if (s < 10) {        s = "0" + s;    }    clock += ":" + s;    return (clock);};// 获取当天日期MyDate.GetToday = function() {    var now = new Date();    var year = now.getFullYear(); // 年    var month = now.getMonth() + 1; // 月    var day = now.getDate(); // 日    var clock = year + "-";    if (month < 10)        clock += "0";    clock += month + "-";    if (day < 10)        clock += "0";    clock += day;    return (clock);};// 获取昨天日期MyDate.GetYesterday = function() {    var date = new Date();    var yesterday_milliseconds = date.getTime() - 1000 * 60 * 60 * 24;    var yesterday = new Date();    yesterday.setTime(yesterday_milliseconds);    var strYear = yesterday.getFullYear();    var strDay = yesterday.getDate();    if(strDay < 10){        strDay = "0"+strDay;    }    var strMonth = yesterday.getMonth() + 1;    if (strMonth < 10) {        strMonth = "0" + strMonth;    }    datastr = strYear + "-" + strMonth + "-" + strDay;    return datastr;};// 获取明天日期MyDate.GetTomorrow = function() {    var date = new Date();    var Tomorrow_milliseconds = date.getTime() + 1000 * 60 * 60 * 24;    var Tomorrow = new Date();    Tomorrow.setTime(Tomorrow_milliseconds);    var strYear = Tomorrow.getFullYear();    var strDay = Tomorrow.getDate();    if(strDay < 10){        strDay = "0"+strDay;    }    var strMonth = Tomorrow.getMonth() + 1;    if (strMonth < 10) {        strMonth = "0" + strMonth;    }    datastr = strYear + "-" + strMonth + "-" + strDay;    return datastr;};// 获取当前年MyDate.GetYear = function() {    var y = new Date();    var year = y.getFullYear();    return year;};// 获取当前月MyDate.GetMonth = function() {    var m = new Date();    var year = m.getFullYear();    var month = m.getMonth() + 1;    if (month < 10) {        month = "0" + month;    }    return year + "-" + month;};// 获取上个月MyDate.GetLastMonth = function() {    var tomonth = new Date();    var year = tomonth.getFullYear(); // 获取当前日期的年    var month = tomonth.getMonth() + 1; // 获取当前日期的月    var year2 = year;    var month2 = parseInt(month) - 1;    if (month2 == 0) {        year2 = parseInt(year2) - 1;        month2 = 12;    }    if (month2 < 10) {        month2 = '0' + month2;    }    var lastmonth = year2 + '-' + month2;    return lastmonth;};// 获取下个月MyDate.GetNextMonth = function() {    var tomonth = new Date();    var year = tomonth.getFullYear(); // 获取当前日期的年    var month = tomonth.getMonth() + 1; // 获取当前日期的月    var year2 = year;    var month2 = parseInt(month) + 1;    if (month2 == 13) {        year2 = parseInt(year2) + 1;        month2 = 1;    }    if (month2 < 10) {        month2 = '0' + month2;    }    var nextmonth = year2 + '-' + month2;    return nextmonth;};

自动填充表单

/** * 华盾自动填充表单 *  * @param {} *            customForm 表单Form元素 * @param {} *            data 要填充的数据对象 */MyUtil.AutoFillForm = function(customForm, data) {    for (var attr in data) {        if (typeof(data[attr]) == 'function') {            continue;        }        var $input = $("input[name='" + attr + "']", customForm);        if (null != $input && $input.length > 0) {            var type = $input.attr("type");            if (type == "checkbox" || type == "radio") {                var avalues = data[attr].split(",");                for (var v = 0; v < avalues.length; v++) {                    $input.each(function(i, n) {                                var value = $(n).val();                                if (value == avalues[v]) {                                    $(n).attr("checked", "checked");                                }                            });                }            } else {                $input.val(data[attr]);            }        } else {            var $select = $("select[name='" + attr + "']", customForm);            if (null != $select && $select.length > 0) {                $select.val(data[attr]);            } else {                var $textarea = $("textarea[name='" + attr + "']", customForm);                if (null != $textarea && $textarea.length > 0) {                    $textarea.val(data[attr]);                } else {                    var $span = $("span[name='" + attr + "']", customForm);                    if (null != $span && $span.length > 0) {                        $span.html(data[attr]);                    }else{                        continue;                    }                }            }        }    }}

 Cookie操作

var Cookies = new Object();// 设置值Cookies.set = function(name, value) {    var argv = arguments;    var argc = arguments.length;    var expires = (argc > 2) ? argv[2] : null;    var path = (argc > 3) ? argv[3] : '/';    var domain = (argc > 4) ? argv[4] : null;    var secure = (argc > 5) ? argv[5] : false;    document.cookie = name + "=" + escape(value)            + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))            + ((path == null) ? "" : ("; path=" + path))            + ((domain == null) ? "" : ("; domain=" + domain))            + ((secure == true) ? "; secure" : "");};// 获取值Cookies.get = function(name) {    var arg = name + "=";    var alen = arg.length;    var clen = document.cookie.length;    var i = 0;    var j = 0;    while (i < clen) {        j = i + alen;        if (document.cookie.substring(i, j) == arg)            return Cookies.getCookieVal(j);        i = document.cookie.indexOf(" ", i) + 1;        if (i == 0)            break;    }    return null;};// 清理Cookies.clear = function(name) {    if (Cookies.get(name)) {        document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";    }};Cookies.getCookieVal = function(offset) {    var endstr = document.cookie.indexOf(";", offset);    if (endstr == -1) {        endstr = document.cookie.length;    }    return unescape(document.cookie.substring(offset, endstr));};

 base-64 编码和解码

function utf8_to_b64( str ) {    return window.btoa(unescape(encodeURIComponent( str )));}function b64_to_utf8( str ) {    return decodeURIComponent(escape(window.atob( str )));}

 

转载于:https://www.cnblogs.com/embraceU/p/9266138.html

你可能感兴趣的文章
linux命令:ls
查看>>
【SAP HANA】关于SAP HANA中带层次结构的计算视图Cacultation View创建、激活状况下在系统中生成对象的研究...
查看>>
【ros】Create a ROS package:package dependencies报错
查看>>
通过容器编排和服务网格来改进Java微服务的可测性
查看>>
灰度图像和彩色图像
查看>>
argparse - 命令行选项与参数解析(转)
查看>>
修改上一篇文章的node.js代码,支持默认页及支持中文
查看>>
java只能的round,ceil,floor方法的使用
查看>>
将txt文件转化为json进行操作
查看>>
我的2014-相对奢侈的生活
查看>>
Java设计模式
查看>>
mysql-This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
查看>>
基本概念复习
查看>>
红黑树
查看>>
【数据库】
查看>>
WindowManager.LayoutParams 详解
查看>>
安卓中数据库的搭建与使用
查看>>
.NET 设计规范--.NET约定、惯用法与模式-2.框架设计基础
查看>>
sql 内联,左联,右联,全联
查看>>
C++关于字符串的处理
查看>>