よく使うjsの方法

11222 ワード

1.       onkeydown="if(event.keyCode==13){checkVendor();}"
2.         window.open(url,'_blank'); 
数日間、数年、数ヶ月後の日付を計算します.

<%-- 
		    
		type 1://        2://        3://       
	--%>
	function calculateDate(type)
	{
		var now_f = new Date().format("yyyyMMdd");
		var datetime;
		switch(type)
		{
			case 1://      
				datetime = new Date(
					parseFloat(Number(now_f.substr(0, 4))+1),
					parseFloat(now_f.substr(4, 2) - 1),
					parseFloat(now_f.substr(6, 2)),
					parseFloat('00'),
					parseFloat('00'),
					parseFloat('00')
				);
			  break;
			case 2://      
				datetime = new Date(
					parseFloat(now_f.substr(0, 4)),
					parseFloat(Number(now_f.substr(4, 2)-1)+6),
					parseFloat(now_f.substr(6, 2)),
					parseFloat('00'),
					parseFloat('00'),
					parseFloat('00')
				);
			  break;
			case 3://       
				datetime = new Date(
					parseFloat(now_f.substr(0, 4)),
					parseFloat(Number(now_f.substr(4, 2)-1)+3),
					parseFloat(now_f.substr(6, 2)),
					parseFloat('00'),
					parseFloat('00'),
					parseFloat('00')
				);
			  break;
		}
		$("#per_valid").val(datetime.format("yyyy-MM-dd"));
	}

/**
 * js bug:   javascript        bug。<br>
 * bug: alert(parseFloat(0.006).toFixed(2));    0.00       0.01
 */
Number.prototype.toFixed = function(d) {
	var s = this + "";
	if (!d)
		d = 0;
	if (s.indexOf(".") == -1)
		s += ".";
	s += new Array(d + 1).join("0");
	if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)) {
		var s = "0" + RegExp.$2, pm = RegExp.$1, a = RegExp.$3.length, b = true;
		if (a == d + 2) {
			a = s.match(/\d/g);
			if (parseInt(a[a.length - 1]) > 4) {
				for ( var i = a.length - 2; i >= 0; i--) {
					a[i] = parseInt(a[i]) + 1;
					if (a[i] == 10) {
						a[i] = 0;
						b = i != 1;
					} else
						break;
				}
			}
			s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"),
					"$1.$2");
		}
		if (b)
			s = s.substr(1);
		return (pm + s).replace(/\.$/, "");
	}
	return this + "";
};

//  Number        
/**
 * js bug:js           bug  。  :1.5451+0.34133
 *          1.88643,  JS        1.88629999999998      
 */
/**
 *   :1.5451+0.34133       :var s = (1.5451).add(0.34133).toFixed(2);
 * alert((7.1).add(12.00027)+"="+(7.1+12.00027));
 */
Number.prototype.add = function(arg) {
	var bit1, bit2, m;
	try {
		bit1 = arg.toString().split(".")[1].length;
	} catch (e) {
		bit1 = 0;
	}
	try {
		bit2 = this.toString().split(".")[1].length;
	} catch (e) {
		bit2 = 0;
	}
	m = Math.pow(10, Math.max(bit1, bit2));
	return (this * m + arg * m) / m;
};
/**
 *   : 1.5451-0.34133       :var s = (1.5451).sub(0.34133);
 */
Number.prototype.sub = function(arg) {
	var bit1, bit2;
	try {
		bit1 = arg.toString().split(".")[1].length;
	} catch (e) {
		bit1 = 0;
	}
	try {
		bit2 = this.toString().split(".")[1].length;
	} catch (e) {
		bit2 = 0;
	}
	var n = Math.max(bit1, bit2);
	var m = Math.pow(10, n);
	return Number(((this * m - arg * m) / m).toFixed(n));
};
/**
 *   : 1.5451*0.34133       :var s = (1.5451).mul(0.34133).toFixed(3);
 */
Number.prototype.mul = function(arg) {
	var bit1, bit2;
	try {
		bit1 = arg.toString().split(".")[1].length;
	} catch (e) {
		bit1 = 0;
	}
	try {
		bit2 = this.toString().split(".")[1].length;
	} catch (e) {
		bit2 = 0;
	}
	var m = bit1 + bit2;
	// var n = (bit1 > bit2) ? bit1 : bit2;
	return (Number(this.toString().replace(".", ""))
			* Number(arg.toString().replace(".", "")) / Math.pow(10, m));// .toFixed(n);
};
/**
 *   : 1.5451/0.34133       :var s = (1.5451).div(0.34133).toFixed(3);
 */
Number.prototype.div = function(arg) {
	var bit1, bit2;
	try {
		bit1 = arg.toString().split(".")[1].length;
	} catch (e) {
		bit1 = 0;
	}
	try {
		bit2 = this.toString().split(".")[1].length;
	} catch (e) {
		bit2 = 0;
	}
	var n = Math.max(bit1, bit2);
	var m = Math.pow(10, n);
	// return (Number(this.toString().replace(".", ""))*m) /
	// (Number(arg.toString().replace(".", ""))*m);
	return ((this * m) / (arg * m));
};

//  String        
/**
 *   :1.5451+0.34133       :var s = (1.5451).add(0.34133).toFixed(2);
 * alert((7.1).add(12.00027)+"="+(7.1+12.00027));
 */
String.prototype.add = function(arg) {
	return Number(this).add(arg);
};
/**
 *   : 1.5451-0.34133       :var s = (1.5451).sub(0.34133);
 */
String.prototype.sub = function(arg) {
	return Number(this).sub(arg);
};
/**
 *   : 1.5451*0.34133       :var s = (1.5451).mul(0.34133).toFixed(3);
 */
String.prototype.mul = function(arg) {
	return Number(this).mul(arg);
};
/**
 *   : 1.5451/0.34133       :var s = (1.5451).div(0.34133).toFixed(3);
 */
String.prototype.div = function(arg) {
	return Number(this).div(arg);
};
/**
 *   html        :var s = "aaa".escape("aaa");
 */
String.prototype.escape = function (html){
	var codeSpan = /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm;
	var codeBlock = /(?:

|^)((?:(?:[ ]{4}|\t).*
+)+)(
*[ ]{0,3}[^ \t
]|(?=~0))/g; var spans = []; var blocks = []; var text = String(html).replace(/\r
/g, '
').replace('/\r/g', '
'); text = '

' + text + '

'; texttext = text.replace(codeSpan, function(code){ spans.push(code); return '`span`'; }); text += '~0'; return text.replace(codeBlock, function(code){ blocks.push(code); return '
\tblock'; }).replace(/&(?!\w+;)/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/`span`/g, function(){ return spans.shift(); }).replace(/
\tblock/g, function(){ return blocks.shift(); }).replace(/~0$/, '').replace(/^

/, '').replace(/

$/, ''); }; // //var flag = false; /** * ImgD: maxWidth: maxHeight: */ function resizeimg(ImgD, maxWidth, maxHeight) { //ImgD.style.display="none"; // , img var image = new Image(); var iwidth = maxWidth; // var iheight = maxHeight; // image.src = ImgD.src; if (image.width > 0 && image.height > 0) { //flag = true; if (image.width / image.height >= iwidth / iheight) { if (image.width > iwidth) { ImgD.width = iwidth; ImgD.height = (image.height * iwidth) / image.width; } else { ImgD.width = image.width; ImgD.height = image.height; } //ImgD.alt = image.width + "×" + image.height; } else { if (image.height > iheight) { ImgD.height = iheight; ImgD.width = (image.width * iheight) / image.height; } else { ImgD.width = image.width; ImgD.height = image.height; } //ImgD.alt = image.width + "×" + image.height; } } centerImage(ImgD, maxWidth, maxHeight); ImgD.style.display="inline"; } /** * Date: * var date = new Date().format('yyyy-MM-dd') */ Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o) { if(new RegExp("("+ k +")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); } } return format; } function centerImage(imgD, maxWidth, maxHeight) { // var div = imgD.parentNode;// div if (imgD.height < maxHeight) { var top = (maxHeight - imgD.height) / 2-2; //ie6 , ie6 if(imgD.height!=0){ imgD.style.marginTop = top + "px"; } } if (imgD.width < maxWidth) { var left = (maxWidth - imgD.width) / 2; //ie6 , ie6 if(imgD.width!=0){ imgD.style.marginLeft = left + "px"; } } } /** * ( :20150415092645) js Date * Date * */ function js_patch_getdate(stime) { var datetime = new Date( parseFloat(stime.substr(0, 4)), parseFloat(stime.substr(4, 2) - 1), parseFloat(stime.substr(6, 2)), parseFloat(stime.substr(8, 2)), parseFloat(stime.substr(10, 2)), parseFloat(stime.substr(12, 2))); return datetime; } function js_patch_fillZero(num, digit) { var str = '' + num; while (str.length < digit) { str = '0' + str; } return str; } function debug_showAttr(obj) { var str="<b>begin:the attribute of "+obj+"</b><br/>"; for(var i in obj) { str+="<b>"+i+"</b>=="+obj[i]+"<br>"; } document.body.innerHTML+=str; } function debug_showMsg(str) { document.body.innerHTML+=str; } function debug_showMsgSrc(str) { document.body.innerHTML+=str.replace(/</g,"&lt").replace(/>/g,"&gt"); }