码迷,mamicode.com
首页 > 编程语言 > 详细

Tool.js(javascript帮助类)

时间:2014-07-28 11:24:20      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   os   io   for   

 //string.format

bubuko.com,布布扣
 $.format = function (source, params) {
            if (arguments.length == 1)
                return function () {
                    var args = $.makeArray(arguments);
                    args.unshift(source);
                    return $.validator.format.apply(this, args);
                };
            if (arguments.length > 2 && params.constructor != Array) {
                params = $.makeArray(arguments).slice(1);
            }
            if (params.constructor != Array) {
                params = [params];
            }
            $.each(params, function (i, n) {
                source = source.replace(new RegExp("\\{" + i + "\\}", "g"), (typeof n == undefined || n == null) ? ‘‘ : n);
            });
            return source;
        }
View Code

//获取URL参数

bubuko.com,布布扣
 function request(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return r[2]; return null;
        }
View Code

//Cookie

bubuko.com,布布扣
 // 设置Cookie
        function setCookie(name, value) {
            var expires = (arguments.length > 2) ? arguments[2] : null;
            document.cookie = name + "=" + encodeURIComponent(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + "";
        }

        // 获取Cookie
        function getCookie(name) {
            var value = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
            if (value != null) {
                return decodeURIComponent(value[2]);
            } else {
                return null;
            }
        }

        // 删除cookie
        function removeCookie(name) {
            var expires = new Date();
            expires.setTime(expires.getTime() - 1000 * 60);
            setCookie(name, "", expires);
        }
View Code

//运算

bubuko.com,布布扣
  // 浮点数加法运算
        function floatAdd(arg1, arg2) {
            var r1, r2, m;
            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));
            return (arg1 * m + arg2 * m) / m;
        }

        // 浮点数减法运算
        function floatSub(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));
            n = (r1 >= r2) ? r1 : r2;
            return ((arg1 * m - arg2 * m) / m).toFixed(n);
        }

        // 浮点数乘法运算
        function floatMul(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);
        }

        //保留2位四舍五入
        function changeTwoDecimal_f(x) {
            var f_x = parseFloat(x);
            if (isNaN(f_x)) {
                alert(function:changeTwoDecimal->parameter error);
                return false;
            }
            var f_x = Math.round(x * 100) / 100;
            var s_x = f_x.toString();
            var pos_decimal = s_x.indexOf(.);
            if (pos_decimal < 0) {
                pos_decimal = s_x.length;
                s_x += .;
            }
            while (s_x.length <= pos_decimal + 2) {
                s_x += 0;
            }
            return s_x;
        }

        // 浮点数除法运算
        function floatDiv(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);
            }
        }

        // 设置数值精度
        function setScale(value, scale, roundingMode) {
            if (roundingMode.toLowerCase() == "roundhalfup") {
                return (Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
            } else if (roundingMode.toLowerCase() == "roundup") {
                return (Math.ceil(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
            } else {
                return (Math.floor(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
            }
        }
View Code

// html字符串转义

bubuko.com,布布扣
 function htmlEscape(htmlString) {
            htmlString = htmlString.replace(/&/g, &amp;);
            htmlString = htmlString.replace(/</g, &lt;);
            htmlString = htmlString.replace(/>/g, &gt;);
            htmlString = htmlString.replace(//g, &acute;);
            htmlString = htmlString.replace(/"/g, ‘&quot;‘);
            htmlString = htmlString.replace(/\|/g, &brvbar;);
            return htmlString;
        }
View Code

 

Tool.js(javascript帮助类),布布扣,bubuko.com

Tool.js(javascript帮助类)

标签:style   blog   http   java   color   os   io   for   

原文地址:http://www.cnblogs.com/wzq806341010/p/3872225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!