Common javascript methods and javascript sharing

Source: Internet
Author: User

Common javascript methods and javascript sharing

We usually write some repetitive js processing code during development. Today we have summarized several common implementation methods. Get Request Parameters and Remove string spaces.

1. get the parameters in the get request

Js Code

Function getPara (para) {if (location. href. indexOf ("? ") =-1) {// if there is no parameter, Do nothing. return null;} else {// GET request? Var urlQuery = location. href. split ("? "); If (urlQuery [1]. indexOf ("&") =-1) {// there is only one parameter if (urlQuery [1]. indexOf ("=") =-1) {// no equal sign, no parameter, Do nothing return null;} else {var keyValue = urlQuery [1]. split ("="); var key = keyValue [0]; var value = keyValue [1]; if (key = para) {return value ;}}} else {// resolution parameter var urlTerms = urlQuery [1]. split ("&"); for (var I = 0; I <urlTerms. length; I ++) {var keyValue = urlTerms [I]. split ("="); var key = keyValue [0]; var value = keyValue [1]; if (key = para) {return value ;}}}} return null ;}

2. // This function is used to remove spaces on the left of the string.

Js Code

function leftTrim(str) {    if (str.charAt(0) == " ") {      str = str.slice(1);      str = leftTrim(str);    }       return str;  }  

3. // This function is used to remove spaces on the right of the string.

Js Code

function rightTrim(str) {    if (str.length - 1 >= 0 && str.charAt(str.length - 1) == " ") {      str = str.slice(0, str.length - 1);      str = rightTrim(str);    }       return str;  } 

4. // convert the time to a fixed format for output

Js Code

/*** Convert the time to a fixed format and output * new Date (). toFormat ('yyyy-MM-dd HH: mm: ss'); * new Date (). toFormat ('yyyy/MM/dd hh: mm: ss'); * Only keywords (yyyy, MM, dd, HH, hh, mm, ss) HH: 24 hours, hh 12 hours */Date. prototype. toFormatString = function (format) {var formatstr = format; if (format! = Null & format! = "") {// Set the year if (formatstr. indexOf ("yyyy")> = 0) {formatstr = formatstr. replace ("yyyy", this. getFullYear ();} // sets the monthly if (formatstr. indexOf ("MM")> = 0) {var month = this. getMonth () + 1; if (month <10) {month = "0" + month;} formatstr = formatstr. replace ("MM", month);} // sets the date if (formatstr. indexOf ("dd")> = 0) {var day = this. getDay (); if (day <10) {day = "0" + day;} formatstr = formatstr. replace ("dd", day);} // sets the hour-24-hour var hours = this. getHours (); if (formatstr. indexOf ("HH")> = 0) {if (month <10) {month = "0" + month;} formatstr = formatstr. replace ("HH", hours);} // if (formatstr. indexOf ("hh")> = 0) {if (hours> 12) {hours = hours-12;} if (hours <10) {hours = "0" + hours;} formatstr = formatstr. replace ("hh", hours);} // you can specify if (formatstr. indexOf ("mm")> = 0) {var minute = this. getMinutes (); if (minute <10) {minute = "0" + minute;} formatstr = formatstr. replace ("mm", minute);} // sets the second if (formatstr. indexOf ("ss")> = 0) {var second = this. getSeconds (); if (second <10) {second = "0" + second;} formatstr = formatstr. replace ("ss", second) ;}return formatstr ;}

The above is all the content of this article. I hope you will like it.

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.