JavaScript common methods to share _javascript skills

Source: Internet
Author: User

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

function Getpara (para) {  
  if (location.href.indexOf ("?") = = 1) {  
   //without arguments, then do nothing.  
   return null;  
  }  
  else{  
   //Get request string  
   var urlquery = Location.href.split ("?").  
   if (Urlquery[1].indexof ("&") ==-1) {///Only one parameter  
    if (urlquery[1].indexof ("=") = = 1) {  
     //no equals 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{  
    //parse 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

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

function Righttrim (str) {  
  if (str.length-1 >= 0 && str.charat (str.length-1) = "") {  
    str = STR.SL Ice (0, str.length-1);  
    str = Righttrim (str);  
  }  
   
  return str;  
} 

4.//Convert time to fixed format output

JS Code

/** * Converts 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 = f  
    Ormatstr.replace ("yyyy", this.getfullyear ());  
      }//Set the month if (Formatstr.indexof ("MM") >=0) {var month = This.getmonth () + 1;  
      if (Month <) {month = "0" + month;  
    } FORMATSTR = Formatstr.replace ("MM", month);  
      //Set Daily if (Formatstr.indexof ("DD") >=0) {var day = This.getday ();  
      if (Day < ten) {day = ' 0 ' + day;  
    } FORMATSTR = Formatstr.replace ("dd", day);  
    }//Set-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 <) {hours = "0" + hours;  
    } FORMATSTR = Formatstr.replace ("hh", hours);  
      //Set the split if (Formatstr.indexof ("mm") >=0) {var minute = this.getminutes ();  
      if (minute <) {minute = "0" + minute;  
    } FORMATSTR = Formatstr.replace ("mm", minute);  
      }//Set the second if (Formatstr.indexof ("ss") >=0) {var second = this.getseconds ();  
      if (second <) {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.