Js Util 도구 모음

10477 단어
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;
}

시간 범위 선별 여기는 제3자 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;
}

좋은 웹페이지 즐겨찾기