javascript common methods to share

  • 2020-06-22 23:55:27
  • OfStack

In view of the current development, we usually write 1 repetitive js processing code, today summarized several more commonly used methods to achieve. Gets the get request parameter, goes to string whitespaces.

Get the parameters in the get request

Js code


function getPara(para){  
  if(location.href.indexOf("?") == -1){  
   //  Without parameters, then Do nothing.  
   return null;  
  }  
  else{  
   //  achieve GET request ? The string following the number   
   var urlQuery = location.href.split("?");  
   if(urlQuery[1].indexOf("&")==-1){// only 1 A 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{  
    //  Analytical parameters   
    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 left space 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 the space on the right side 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 time into fixed format output

Js code


/** 
*  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'); 
*  Keyword only support ( yyyy , MM , dd , HH , hh , mm , ss ) HH Said: 24 Hours, hh said 12 hours  
*/  
Date.prototype.toFormatString=function(format){  
  var formatstr = format;  
  if(format != null && format != ""){  
    // Set up in   
    if(formatstr.indexOf("yyyy") >=0 ){  
      formatstr = formatstr.replace("yyyy",this.getFullYear());  
    }  
    // Set the month   
    if(formatstr.indexOf("MM") >=0 ){  
      var month = this.getMonth() + 1;  
      if(month < 10){  
        month = "0" + month;  
      }  
      formatstr = formatstr.replace("MM",month);  
    }  
    // Set the date   
    if(formatstr.indexOf("dd") >=0 ){  
      var day = this.getDay();  
      if(day < 10){  
        day = "0" + day;  
      }  
      formatstr = formatstr.replace("dd",day);  
    }  
    // When setting  - 24 hours   
    var hours = this.getHours();  
    if(formatstr.indexOf("HH") >=0 ){  
      if(month < 10){  
        month = "0" + month;  
      }  
      formatstr = formatstr.replace("HH",hours);  
    }  
    // When setting  - 12 hours   
    if(formatstr.indexOf("hh") >=0 ){  
      if(hours > 12){  
        hours = hours - 12;  
      }  
      if(hours < 10){  
        hours = "0" + hours;  
      }  
      formatstr = formatstr.replace("hh",hours);  
    }  
    // Set points   
    if(formatstr.indexOf("mm") >=0 ){  
      var minute = this.getMinutes();  
      if(minute < 10){  
        minute = "0" + minute;  
      }  
      formatstr = formatstr.replace("mm",minute);  
    }  
    // Set up the second   
    if(formatstr.indexOf("ss") >=0 ){  
      var second = this.getSeconds();  
      if(second < 10){  
        second = "0" + second;  
      }  
      formatstr = formatstr.replace("ss",second);  
    }  
  }  
  return formatstr;  
} 

This is the end of this article, I hope you enjoy it.


Related articles: