JavaScript utility functions

Source: Internet
Author: User
Tags rtrim

1. Remove string spaces

 /*   go left space   function   LTrim (s) { return  s.replace (/^ (\s*| *)/, " /*   go right space  */ function   RTrim (s) { return  s.replace (/(\s*| *) $/, ""  /*   go to left and right space  */ function   Trim (s) {  LTrim (RTrim (s));}  

2. Get URL Address bar parameters

function Geturlqueryparam (name) {    varnew RegExp ("(^|&)" + name + "= ([^&]*) (&|$)", " I ");     var r = window.location.search.substr (1). Match (reg);     var value = ';     if NULL {        = decodeuricomponent (r[2]);    }     return value;}

3. Digital thousand-point conversion

/*number format conversions thousands of bits*/functionnumtomicrometer (num) {if(Trim (num + "") = = "") {        return""; }    if(IsNaN (num)) {return""; } num= num + ""; if(/^.*\.. *$/. Test (num)) {Varpointindex= Num.lastindexof ("."); Varintpart= num.substring (0, PointIndex); Varpointpart= num.substring (PointIndex + 1, num.length); Intpart= Intpart + ""; varRe =/(-?\d+) (\d{3})/ while(Re.test (Intpart)) {Intpart= Intpart.replace (Re, "$1,$2")} num= Intpart + "." +Pointpart; } Else{num= num + ""; varRe =/(-?\d+) (\d{3})/ while(Re.test (num)) {num= Num.replace (Re, "$1,$2")        }    }    returnnum;}/*remove thousands of bits*/functiondelmicrometer (num) {if(Trim (num + "") = = "") {        return""; } num= Num.replace (/,/gi, "); returnnum;}

4. Get the length of bytes occupied by a given string

functionGetbytelen (val) {//Pass in a string    varLen = 0;  for(vari = 0; i < val.length; i++) {        if(Val[i].match (/[^\x00-\xff]/ig)! =NULL)//Full WidthLen + 2;//if it's full-width, take two bytes        ElseLen+ = 1;//half-width occupies one byte    }    returnLen;}

5. Building URL Parameters

/*param = {name1:value1, name2:value2, ...}*/functionBuildparam (param) {varParamcontent = ' '; Try {        if(!empty (param)) {             for(varKeyinchparam) {paramcontent+ = key + ' = ' +encodeURIComponent (Param[key])+ ' & '; }        }    } Catch(e) {}returnparamcontent;}

6. Determine if the variable is null undefined, NULL, ', FALSE, 0, [], {} return True, otherwise false

functionEmpty (v) {Switch(typeofv) { Case' Undefined ' :            return true;  Case' String ' :            if(Trim (v). length = = 0)                return true;  Break;  Case' Boolean ' :            if(!v)return true;  Break;  Case' Number ' :            if(0 = = =v)return true;  Break;  Case' Object ' :            if(NULL===v)return true; if(Undefined!== v.length && v.length = = 0)                return true;  for(varKinchv) {return false; }            return true;  Break; }    return false;}

7. Remove HTML tags

/* Remove HTML Tags */ function Removehtmltag (str) {    return str.replace (/<[^>]+>/g, "");}

8. Get URL parameters (return array type)

functionGetrequestparam (param) {varurl = location.search;//gets the URL in the "?" String after the character    varParamstr =NewArray (); if(Url.indexof ("?")! =-1) {        varstr = URL.SUBSTR (1); STRs= Str.split ("&");  for(vari = 0; i < strs.length; i++) {            if(param = = strs[i].split ("=") [0]) {Paramstr.push (Strs[i].split ("=") [1]); }        }    }    returnparamstr;}

9. Specify the character Fugauliang

functionsethighlighttext (TXT, currenttargettext) {if(Empty (TXT)) {return‘‘; }    varNewtxt =txt; if(Currenttargettext.length! = 0) {$ (Currenttargettext). each (function(i) {varReg =NewREGEXP (Currenttargettext[i], "G"); Newtxt= Newtxt.replace (Reg, "<em>" +Currenttargettext[i]+ "</em>");    }); }    returnNewtxt;}

10. Replace the specified parameter in the URL

functionchangeurlarg (URL, paramname, paramname_val) {varPattern = paramname + ' = ([^&]*) '; varReplaceText = paramname + ' = ' +Paramname_val; if(Url.match (pattern)) {varTMP = '/(' + paramname + ' =) ([^&]*)/gi '; TMP=Url.replace (eval (TMP), replacetext); returntmp; } Else {        if(Url.match (' [\?] '))) {            returnURL + ' & ' +ReplaceText; } Else {            returnURL + '? ' +ReplaceText; }    }    returnURL + ' \ n ' + paramname + ' \ n ' +Paramname_val;}

JavaScript utility functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.