Home  >  Article  >  Web Front-end  >  Compilation of various commonly used JS functions_Basic knowledge

Compilation of various commonly used JS functions_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:18:47954browse

Js gets the page address parameter

Copy code The code is as follows:

function getUrlPara(paraName)
{
var sUrl = location.href;
var sReg = "(?://?|&){1}" paraName "=([^&]*)"
var re = new RegExp(sReg, "gi");
re.exec(sUrl);
return RegExp.$1;
}

Address jump
Copy code The code is as follows:

var pn = $("#gotopagenum").val();//#gotopagenum It is the id attribute of the text box
location.href = "NewList.aspx?pagenum=" pn;//location.href realizes the jump of the client page

Thousandth place
Copy code The code is as follows:

function Convert(money)
{
var s = money; //Get decimal data
s = "";
if (s.indexOf(".") == -1) s = ".00"; //If there is no decimal point, add it at the end decimal point and 00
if (/.d$/.test(s)) s = "0"; //Regular judgment
while (/d{4}(.|,)/.test(s )) //Replace if conditions are met
s = s.replace(/(d)(d{3}(.|,))/, "$1,$2"); //Add every 3rd digit
return s;
}

Judge whether it is a number
Copy code The code is as follows :

function IsNumeric(txt) {
if (txt == "") {
return false;
}

if (txt.indexOf( ",") > 0) {
txt = txt.replace(",", "");
}

if (isNaN(txt)) {
return false;
}
else {
return true;
}
}

Format the number to two decimal places
Copy code The code is as follows:

function changeTwoDecimal_f(x) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {
alert('function:changeTwoDecimal->parameter error');
return false;
}
f_x = Math.round(f_x * 100) / 100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x = '.';
}
while (s_x.length <= pos_decimal 2) {
s_x = '0';
}
return s_x;
}

Js function parseFloat parseInt for numerical operations

js current date yyyy-mm-dd preset query conditions
Copy code The code is as follows:

var now = new Date();
var year = now.getYear();

if (now .getYear() < 1900) {
year = now.getYear() 1900;
}

var month = now.getMonth() 1;
var day = now.getDate ();

if (month < 10) month = "0" month;
if (day < 10) day = "0" day;

$("# txtDate1").val(year.toString() "-" month.toString() "-01");
$("#txtDate2").val(year.toString() "-" month.toString( ) "-" day.toString());

Js gets the timestamp, replacing Guid in some scenarios
Copy code The code is as follows:

function NowTimeCode()
{
var Result="";

var now = new Date() ;

var year = now.getYear();

if (now.getYear() < 1900) {
year = now.getYear() 1900;
}

var month = now.getMonth() 1;
var day = now.getDate();
var hour = now.getHours();
var minutes = now.getMinutes( );
var second = now.getSeconds();
var millisecond = now.getMilliseconds();

if (month < 10) month = "0" month;
if (day < 10) day = "0" day;
if (hour < 10) hour = "0" hour;
if (minutes < 10) minutes = "0" minutes;
if (second < 10) second = "0" second;

if (millisecond < 10)
millisecond = "00" millisecond;
else
{
if ( millisecond < 100)
{
millisecond = "0" millisecond;
}
}

Result = year.toString() month.toString() day.toString() hour.toString() minutes.toString() second.toString() millisecond.toString();

return Result;

}
Statement:
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