Js Util工具集

JSON 数据还原 防止后台JSON转换异常

var FastJson = {
    isArray: function(a) {
        return "object" == typeof a && "[object array]" == Object.prototype.toString.call(a)
            .toLowerCase();
    },
    isObject: function(a) {
        return "object" == typeof a && "[object object]" == Object.prototype.toString.call(a)
            .toLowerCase();
    },
    format: function(a) {
        if (null == a)
            return null;
        "string" == typeof a && (a = eval("(" + a + ")"));
        return this._format(a, a, null, null, null);
    },
    _randomId: function() {
        return "randomId_" + parseInt(1E9 * Math.random());
    },
    _getJsonValue: function(a, c) {
        var d = this._randomId(),
            b;
        b = "" + ("function " + d + "(root){") + ("return root." + c + ";");
        b += "}";
        b += "";
        var e = document.createElement("script");
        e.id = d;
        e.text = b;
        document.body.appendChild(e);
        d = window[d](a);
        e.parentNode.removeChild(e);
        return d;
    },
    _format: function(a, c, d, b, e) {
        d || (d = "");
        if (this.isObject(c)) {
            if (c.$ref) {
                var g = c.$ref;
                0 == g.indexOf("$.") && (b[e] = this._getJsonValue(a, g.substring(2)));
                return
            }
            for (var f in c)
                b = d, "" != b && (b += "."), g = c[f], b += f, this
                ._format(a, g, b, c, f);
        } else if (this.isArray(c))
            for (f in c)
                b = d, g = c[f], b = b + "[" + f + "]", this._format(a, g,
                    b, c, f);
        return a;
    }
};
isArray: function(a) {
        return "object" == typeof a && "[object array]" == Object.prototype.toString.call(a)
            .toLowerCase();
    },
    isObject: function(a) {
        return "object" == typeof a && "[object object]" == Object.prototype.toString.call(a)
            .toLowerCase();
    },
    format: function(a) {
        if (null == a)
            return null;
        "string" == typeof a && (a = eval("(" + a + ")"));
        return this._format(a, a, null, null, null);
    },
    _randomId: function() {
        return "randomId_" + parseInt(1E9 * Math.random());
    },
    _getJsonValue: function(a, c) {
        var d = this._randomId(),
            b;
        b = "" + ("function " + d + "(root){") + ("return root." + c + ";");
        b += "}";
        b += "";
        var e = document.createElement("script");
        e.id = d;
        e.text = b;
        document.body.appendChild(e);
        d = window[d](a);
        e.parentNode.removeChild(e);
        return d;
    },
    _format: function(a, c, d, b, e) {
        d || (d = "");
        if (this.isObject(c)) {
            if (c.$ref) {
                var g = c.$ref;
                0 == g.indexOf("$.") && (b[e] = this._getJsonValue(a, g.substring(2)));
                return
            }
            for (var f in c)
                b = d, "" != b && (b += "."), g = c[f], b += f, this
                ._format(a, g, b, c, f);
        } else if (this.isArray(c))
            for (f in c)
                b = d, g = c[f], b = b + "[" + f + "]", this._format(a, g,
                    b, c, f);
        return a;
    }
};

POST提交

function post(URL, PARAMS) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";
    for (var x in PARAMS) {
        var opt = document.createElement("textarea");
        opt.name = x;
        opt.value = PARAMS[x];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}

时间转换JS

function getDateTime(data) {
    if (data == undefined) {
        return "";
    }
    var d = new Date(data); //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" + (d.getMonth() + 1) + "-" + (d.getDate());
    return date;
}

具体时间转换 精确到秒

function getDateTimeHours(data) {
    if (data == undefined) {
        return "";
    }
    var d = new Date(data); //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" + (d.getMonth() + 1) + "-" + (d.getDate()) + " " + (d.getHours()) + ":" + (d.getMinutes()) + ":" + (d.getSeconds());
    return date;
}

具体时间 精确到分

function getDateTimeMinute(data) {
    if (data == undefined) {
        return "";
    }
    var d = new Date(data); //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" + (d.getMonth() + 1) + "-" + (d.getDate()) + " " + (d.getHours()) + ":" + (d.getMinutes());
    return date;
}

时间范围筛选 这里需要引入第三方datepicker JS插件

function DatePicker(beginSelector, endSelector) {
    //仅选择日期
    $(beginSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0,
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false,
        endDate: new Date()
    }).on('changeDate', function(ev) {
        if (ev.date) {
            $(endSelector).datepicker('setStartDate', new Date(ev.date.valueOf()));
            $(beginSelector).datepicker('hide');
            $(endSelector).datepicker('show');
        } else {
            $(endSelector).datepicker('setStartDate', null);
        }
    })
    $(endSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0,
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false,
        endDate: new Date()
    }).on('changeDate', function(ev) {
        if (ev.date) {
            $(beginSelector).datepicker('setEndDate', new Date(ev.date.valueOf()))
        } else {
            $(beginSelector).datepicker('setEndDate', new Date());
        }
    })
}

格式化金额 保留后2位小数

function formatCurrency(num) {
    if (num == undefined || num == '') {
        return 0.00;
    }
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}

时间范围筛选 可以选中未来时间

function DatePicker2(beginSelector, endSelector) {
    //仅选择日期
    $(beginSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0,
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false
    }).on('changeDate', function(ev) {
        if (ev.date) {
            $(endSelector).datepicker('setStartDate', new Date(ev.date.valueOf()));
            $(beginSelector).datepicker('hide');
            $(endSelector).datepicker('show');
        } else {
            $(endSelector).datepicker('setStartDate', null);
        }
    })
    $(endSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0,
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false
    }).on('changeDate', function(ev) {
        if (ev.date) {
            $(beginSelector).datepicker('setEndDate', new Date(ev.date.valueOf()))
        } else {
            $(beginSelector).datepicker('setEndDate', new Date());
        }
    })
}

时间范围筛选 今天开始 月份

function DatePicker3(endSelector) {
    $(endSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        todayHighlight: true,
        format: 'yyyy-mm',
        startView: 3,
        minViewMode: 1,
        maxViewMode: 3,
        forceParse: 0,
    }).datepicker('setStartDate', new Date());
}

时间范围筛选 今天开始 日期

function DatePicker4(endSelector) {
    //仅选择日期
    $(endSelector).datepicker({
        language: "zh-CN",
        autoclose: true,
        startView: 0
        format: "yyyy-mm-dd",
        clearBtn: true,
        todayBtn: false
    }).datepicker('setStartDate', new Date());
}
function showdatepicker(endSelector) {
    $(endSelector).datepicker({
        language: "zh-CN",
        monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],  区域化月名为中文
        prevText: '上月',  //前选按钮提示
        nextText: '下月',  //后选按钮提示
        changeYear: true,  //年下拉菜单
        changeMonth: true,  //月下拉菜单
        showButtonPanel: true,  //显示按钮面板
        showMonthAfterYear: true,  //月份显示在年后面
        currentText: "本月",  //当前日期按钮提示文字
        closeText: "关闭",  //关闭按钮提示文字
        dateFormat: "yy-mm",  //日期格式
    });
}

AJAX 同步

function sync(url, PARAMS) {
    return ajax_util(url, PARAMS, false);
}
//AJAX 异步
function async(url, PARAMS) {
    return ajax_util(url, PARAMS, true);
}
//AJAX工具JS
function ajax_util(url, PARAMS, is_a_s_ync) {
    var _data;
    $.ajax({
        cache: false,
        type: "POST",
        url: contextPath + url,
        data: PARAMS,
        async: is_a_s_ync,
        error: function(request) {
            alert("Connection error");
        },
        success: function(data) {
            _data = FastJson.format(data);
        },
        error: function() {
            alert("Ajax请求数据失败!");
        }
    });
    return _data;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容

  • AJAX 原生js操作ajax 1.创建XMLHttpRequest对象 var xhr = new XMLHtt...
    碧玉含香阅读 3,201评论 0 7
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,133评论 25 707
  • 1.几种基本数据类型?复杂数据类型?值类型和引用数据类型?堆栈数据结构? 基本数据类型:Undefined、Nul...
    极乐君阅读 5,517评论 0 106
  • 推荐10套最适合孩子阅读的少儿英语书。 英语作为全球通用语言,学习的重要性不言而喻,少儿是人类成长学习的最佳时期,...
    黑鸿升阅读 17,664评论 0 3
  • 文/爱晓贱 所有的花言巧语都得绞尽脑汁的酝酿 姑娘,听说有一种眼神叫看见你就发光 全部的放荡不羁都想恰到好处的隐藏...
    落雪有晴空阅读 871评论 10 6