JavaScript常用方法

來源:互聯網
上載者:User

標籤:log   mat   gets   exp   尾碼   filename   pod   cap   cape   

判斷運行用戶端
    function isPhone() {        var flag = false;        var userAgentInfo = navigator.userAgent;        var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];        for (var i = 0; i < Agents.length; i++) {            if (userAgentInfo.indexOf(Agents[i]) > 0) {                flag = true;                break;            }        }        return flag;    }
計算帶中文的字串長度(中文2字元)
    function getRealLength(str) {        str = String(str);        var len = str.match(/[\u4e80-\u9fff]/g).length;        return str.length += len;    }    getRealLength('asd你好'); //7
    String.prototype.ChineseLength = function () {        return this.replace(/[^\x00-\xff]/g, "**").length;    };    'asd你好'.ChineseLength(); //7
時間格式化
    function myFormatTime(fmt, date) {        var o = {            "M+": date.getMonth() + 1,  //月份            "d+": date.getDate(),   //日            "h+": date.getHours(),  //小時            "m+": date.getMinutes(),    //分            "s+": date.getSeconds(),    //秒            "q+": Math.floor((date.getMonth() + 3) / 3),    //季度            "S": date.getMilliseconds() //毫秒        };        if (/(y+)/.test(fmt))            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));        for (var k in o)            if (new RegExp("(" + k + ")").test(fmt))                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));        return fmt;    }    myFormatTime("yyyy年MM月dd日 hh:mm:ss:S 第q季度", new Date());     // "2018年03月12日 11:22:42:263 第1季度"
檔案大小格式化
    function bytesToSize(bytes) {        if (bytes == 0) return '0 B';        var k = 1024, sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],            i = Math.floor(Math.log(bytes) / Math.log(k));        return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];    }    bytesToSize(124213412); // "118 MB"
cookie擷取
    function getcookie(objname) {        var arrstr = document.cookie.split("; ");        for (var i = 0; i < arrstr.length; i++) {            var temp = arrstr[i].split("=");            if (temp[0] == objname) return unescape(temp[1]);        }    }    getcookie('cookieName'); //'cookieVal'
擷取檔案尾碼名
    function GetExtensionFileName(pathfilename){        var reg = /(\\+)/g;        var pfn = pathfilename.replace(reg, "#");        var arrpfn = pfn.split("#");        var fn = arrpfn[arrpfn.length - 1];        var arrfn = fn.split(".");        return arrfn[arrfn.length - 1];    }    GetExtensionFileName('File.zip'); // zip

未完待更新

JavaScript常用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.