Home > Web Front-end > JS Tutorial > Sharing commonly used methods in javascript_javascript skills

Sharing commonly used methods in javascript_javascript skills

WBOY
Release: 2016-05-16 15:52:11
Original
1375 people have browsed it

In view of the fact that everyone will write some repetitive js processing code in daily development, today I have summarized several common methods to implement it. Get the 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){  
   // 没有参数,则Do nothing.  
   return null;  
  }  
  else{  
   // 取得GET请求?号后面的字符串  
   var urlQuery = location.href.split("?");  
   if(urlQuery[1].indexOf("&")==-1){//只有一个参数  
    if (urlQuery[1].indexOf("=") == -1) {  
     //没有等号,没有参数,则Do nothing  
     return null;  
    }else{  
     var keyValue = urlQuery[1].split("=");  
     var key   = keyValue[0];  
     var value  = keyValue[1];  
     if(key==para){  
      return value;  
     }  
    }  
   }else{  
    // 解析参数  
    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;  
  }  
Copy after login

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

Js code

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

3. //This function is used to remove the spaces 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;  
} 
Copy after login

4. //Convert time into fixed format for output

Js code

/** 
* 将时间转换成固定格式输出 
* new Date().toFormat('yyyy-MM-dd HH:mm:ss'); 
* new Date().toFormat('yyyy/MM/dd hh:mm:ss'); 
* 只支持关键字(yyyy、MM、dd、HH、hh、mm、ss)HH:表示24小时,hh表示12小时 
*/  
Date.prototype.toFormatString=function(format){  
  var formatstr = format;  
  if(format != null && format != ""){  
    //设置年  
    if(formatstr.indexOf("yyyy") >=0 ){  
      formatstr = formatstr.replace("yyyy",this.getFullYear());  
    }  
    //设置月  
    if(formatstr.indexOf("MM") >=0 ){  
      var month = this.getMonth() + 1;  
      if(month < 10){  
        month = "0" + month;  
      }  
      formatstr = formatstr.replace("MM",month);  
    }  
    //设置日  
    if(formatstr.indexOf("dd") >=0 ){  
      var day = this.getDay();  
      if(day < 10){  
        day = "0" + day;  
      }  
      formatstr = formatstr.replace("dd",day);  
    }  
    //设置时 - 24小时  
    var hours = this.getHours();  
    if(formatstr.indexOf("HH") >=0 ){  
      if(month < 10){  
        month = "0" + month;  
      }  
      formatstr = formatstr.replace("HH",hours);  
    }  
    //设置时 - 12小时  
    if(formatstr.indexOf("hh") >=0 ){  
      if(hours > 12){  
        hours = hours - 12;  
      }  
      if(hours < 10){  
        hours = "0" + hours;  
      }  
      formatstr = formatstr.replace("hh",hours);  
    }  
    //设置分  
    if(formatstr.indexOf("mm") >=0 ){  
      var minute = this.getMinutes();  
      if(minute < 10){  
        minute = "0" + minute;  
      }  
      formatstr = formatstr.replace("mm",minute);  
    }  
    //设置秒  
    if(formatstr.indexOf("ss") >=0 ){  
      var second = this.getSeconds();  
      if(second < 10){  
        second = "0" + second;  
      }  
      formatstr = formatstr.replace("ss",second);  
    }  
  }  
  return formatstr;  
} 
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template