JavaScript common method of sharing

Source: Internet
Author: User
Tags split

This article to share a few JavaScript commonly used methods, very practical, but also very simple, the need for small partners can refer to.

For now everyone in peacetime development, will write a number of Repetitive JS processing code, today summed up several more commonly used methods to achieve. Gets the GET request parameter, going to the string space.

1. Getting the parameters in GET request

JS Code

?

1, 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 function Getpara (para) {if (Location.href.indexOf ("?") = = 1) {///No arguments, then does nothing. return null;} else{//Obtain the string following the GET request number var urlquery = Location.href.split ("?"); if (Urlquery[1].indexof ("&") ==-1) {///only one parameter if (urlquery[1].indexof ("=") = = 1) {//No equal sign, no arguments, then do nothing return null ; }else{var keyvalue = urlquery[1].split ("="); var key = keyvalue[0]; var value = keyvalue[1]; if (Key==para) {return value; }}else{//parsing 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 the space left by the string

JS Code

?

1 2 3 4 5 6 7 8 function Lefttrim (str) {if (Str.charat (0) = = "") {str = str.slice (1); str = Lefttrim (str); return str; }

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

JS Code

?

1 2 3 4 5 6 7 8 function Righttrim (str) {if (str.length-1 >= 0 && str.charat (str.length-1) = "") {str = str.slice (0, St R.LENGTH-1); str = Righttrim (str); return str; }

4.//Convert time to fixed format output

JS Code

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 The /** * Converts the time to a fixed format output * New Date (). Toformat (' Yyyy-mm-dd HH:mm:ss '); New Date (). Toformat (' Yyyy/mm/dd hh:mm:ss '); * Only support keywords (yyyy, MM, DD, hh, hh, MM, ss) HH: For 24 hours, HH said 12 hours/date.prototype.toformatstring=function (format) {var formatstr = format; if (format!= null && format!= "") {//Set year if (Formatstr.indexof ("yyyy") >=0) {formatstr = Formatstr.replace ("yy yy ", This.getfullyear ()); }//Set month if (Formatstr.indexof ("MM") >=0) {var month = This.getmonth () + 1; if (Month <) {month = "0" + month;} fo Rmatstr = Formatstr.replace ("MM", month); }//Set Date if (Formatstr.indexof ("DD") >=0) {var day = This.getday (); if (<) {days = "0" + daily;} formatstr = Form Atstr.replace ("DD", day); }//Set when -24 hours var hours = this.gethours (); if (Formatstr.indexof ("hh") >=0) {if (month <) {month = "0" + month;} formatstr = Formatstr.replace ("hh", hours); }//Set-12 hours if (Formatstr.indexof ("hh") >=0) {if (Hours >) {hours = hours-12;} if (Hours < ten) {hours = "0 "+ hours;} Formatstr = Formatstr.replace ("hh", hours); //Set the IF (Formatstr.indexof ("mm") >=0) {var minute = this.getminutes (); if (minute < ten) {minute = "0" + minute;} Formatstr = Formatstr.replace ("mm", minute); }//Set seconds if (Formatstr.indexof ("ss") >=0) {var second = This.getseconds (); if (second < ten) {second = "0" + second;} Formatstr = Formatstr.replace ("ss", second); } return FORMATSTR; }

The above is the entire contents of this article, I hope you can enjoy.

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.