首頁  >  文章  >  web前端  >  javascript學習筆記之10個原生技巧_javascript技巧

javascript學習筆記之10個原生技巧_javascript技巧

WBOY
WBOY原創
2016-05-16 16:47:261179瀏覽

1、原生JavaScript實作字串長度截取

複製程式碼 程式碼如下:

function cutstr(str. temp;
    var icount = 0;
    var patrn = /[^x00-xff]/;
    var strre = "";
 i ) {
        if (icount             temp = str.substr ) {
                icount = icount 1
            } else {
                       strre = temp
        } else {
           strre ". .."
}



2、原生JavaScript取得網域名稱主機



複製碼

代碼如下:

function getHosthost(url) {  "null";    if(typeof url == "undefined"|| null == url) {        url = window.location.href /([^/]*).*/;    var match = url.match(regex);
    if(typeof match != "undefined" && null [1];
    }
    return host;
}



3、原生JavaScript清除空格





複製代碼
代碼如下:

String.prototype.trim = function() {

    var reExtraSpace = /^s*(.*?)s $/;    return this.replace(reExtraSpace, "$1")} 4、原生JavaScript替換全部




複製程式碼
程式碼如下:

String.prototype.replaceAll = function(s1, sString.prototype.replaceAll = function(s1, sString.prototype.replaceAll = function(s1, sString.prototype.replaceAll = function(s1, sString.prototype.replaceAll = function(s1, sString.prototype.replaceAll = function(s1, sString.prototype.replaceAll = function(s1, sString) {

    return this.replace(new RegExp(s1, "gm"), s2)} 5、原生JavaScript轉義html標籤



複製程式碼
代碼如下:

function Htmlcode(text) {

/g, '>')


複製程式碼

代碼如下:

function HtmlDecode(text) {/g, '>')

}


複製程式碼


程式碼如下:

Date.prototype.Format = function(formatStr) {
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四三' ', '五', '六'];
    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/yy|YY/, (this .getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' (this.getYear() % 100));
    str = str.replace(/MM / , (this.getMonth() 1) > 9 ? (this.getMonth() 1).toString() : '0' (this.getMonth() 1));
    str = str.replace(/ M/ g, (this.getMonth() 1));
    str = str.replace(/w|W/g, Week[this.getDay()]);
    str = str.replace(/ dd| DD/, this.getDate() > 9 ? this.getDate().toString() : '0' this.getDate());
    str = str.replace(/d|D/g, this.getDate ());
    str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' this.getHours()) ;
str = str.replace(/h|H/g, this.getHours());
    str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes( ).toString() : '0' this.getMinutes());
    str = str.replace(/m/g, this.getMinutes());
    str = str.replace(/ss|SS /, this.getSeconds () > 9? >    回str
}


8.逗號JavaScript判斷是否為數字類型

複製程式碼程式碼如下:
function isDigit(value) {  /^[0-9]*$/;
    if (patrn.exec(value) == null || value == "") {
        return false        return false       
    }
}



9、原生JavaScript設定cookie值

複製程式碼

程式碼如下:function setCookiename, value, Hours) {    var d = new Date();    var d = new Date();    var d = new Date();
    var d = new Date();
    var d = new Date();
    var d = new Date();
    var d = new Date();
    var var d = new Date();
    var offset = 8;
    var utc = d.getTime() (d.getTimezoneOffset() * 60000);量) ;
    var exp = new Date(nd);
    exp.setTime(exp.getTime() 小時* 60 * 60 * 1000);
  ) ) " ;path=/;expires=" exp.toGMTString() ";domain=360doc.com;"
}

10、JavaScript取得cookie值

複製程式碼程式碼如下:

function getCookiename) {   document.cookie.match(new RegExp("(^| )" name "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2 ]);
   回傳null
}

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn