<h3>รวม javascript utils à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¸£à¸£à¸à¸à¸£à¸°à¹à¸¢à¸à¸à¹à¸à¸µà¹à¹à¸à¹à¸à¸²à¸à¸à¹à¸à¸¢</h3>
<p></br><br />
<strong>รวม javascript utils à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¸£à¸£à¸à¸à¸£à¸°à¹à¸¢à¸à¸à¹à¸à¸µà¹à¹à¸à¹à¸à¸²à¸à¸à¹à¸à¸¢</strong><br />
à¸à¸±à¸à¸à¹à¸à¸±à¹à¸ Utils สำหรัà¸à¸à¸²à¸£à¹à¸à¸µà¸¢à¸à¹à¸à¸£à¹à¸à¸£à¸¡à¸à¸·à¸à¸à¸²à¸£à¸£à¸§à¸à¸£à¸§à¸¡à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¸£à¸£à¸à¸à¸£à¸°à¹à¸¢à¸à¸à¹à¸à¸µà¹à¹à¸à¹à¸à¸²à¸à¸à¹à¸à¸¢ ๠มาสรà¹à¸²à¸à¹à¸à¹à¸à¸à¸¥à¸²à¸ªà¸à¸·à¹à¸à¸§à¹à¸² utils à¹à¸¥à¸°à¸à¸³à¹à¸à¹à¸à¹à¸à¸²à¸à¹à¸à¸à¸¥à¸²à¸ªà¸à¸·à¹à¸à¸«à¸£à¸·à¸à¸ªà¹à¸§à¸à¸à¹à¸²à¸ ๠à¹à¸à¸à¸±à¹à¸à¸à¸à¸à¸à¸à¸à¸à¸²à¸£à¸à¸±à¸à¸à¸² สำหรัà¸à¸ าษา javascript à¹à¸£à¸²à¸ªà¸²à¸¡à¸²à¸£à¸à¸ªà¸£à¹à¸²à¸à¹à¸à¸¥à¹ <!--more-->utils.js à¹à¸à¸·à¹à¸à¹à¸à¹à¸à¸£à¸§à¸à¸£à¸§à¸¡à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¸£à¸£à¸à¸à¸£à¸°à¹à¸¢à¸à¸à¹à¸à¹à¸²à¸ ๠à¹à¸§à¹à¹à¸à¹à¸à¸²à¸à¹à¸à¹à¹à¸à¹à¸à¸à¸±à¸ à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¹à¸²à¸ ๠à¸à¸µà¹à¹à¸£à¸²à¸¡à¸±à¸à¸à¸°à¹à¸à¹à¸à¸²à¸à¸à¸¢à¸¹à¹à¸à¹à¸à¸¢ ๠à¹à¸à¸à¸²à¸£à¹à¸à¸µà¸¢à¸à¸ าษา javascript<br />
</br></p>
<blockquote><p><a href="https://www.codebee.co.th/labs/examples/javascriptUtils.zip">à¸à¸²à¸§à¸à¹à¹à¸«à¸¥à¸ Utils.js</a></p></blockquote>
<p></br></p>
<h4>à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¸²à¸£à¸à¸³à¸à¸§à¸à¹à¸à¸à¸£à¹à¹à¸à¹à¸à¸à¹à¹à¸ javascript</h4>
<pre class="lang:default decode:true " > 
function calPercentag(percent,amount){ 
	var discount = (percent*amount)/100; 
	return discount; 
} 
</pre>
<p><strong>à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸ à¹à¸¡à¸·à¹à¸à¹à¸£à¸²à¸à¹à¸à¸à¸à¸²à¸£à¸£à¸¹à¹à¸§à¹à¸² 10% à¸à¸à¸ 120 à¸à¸·à¸à¹à¸à¹à¸²à¹à¸«à¸£à¹</strong></p>
<pre class="lang:default decode:true " >calPercentag(10,120); //à¸à¸¥à¸¥à¸±à¸à¸à¹ = 12</pre>
<p></br></p>
<h4>à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¹à¸à¸´à¹à¸¡à¹à¸à¸£à¸·à¹à¸à¸à¸«à¸¡à¸²à¸¢ comma สำหรัà¸à¸à¸±à¸§à¹à¸¥à¸</h4>
<pre class="lang:default decode:true " > 
function addComma(x) { 
 var parts = x.toString().split("."); 
 parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
 return parts.join("."); 
} 
</pre>
<p><strong>à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸ à¸à¸±à¸§à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¸°à¹à¸à¸¥à¸µà¹à¸¢à¸à¸à¸²à¸£à¸²à¸¡à¸´à¹à¸à¸à¸£à¹ number à¸à¸µà¹à¸ªà¹à¸à¹à¸à¹à¸²à¸¡à¸²à¹à¸à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¸à¸à¹à¸à¹à¸à¹à¸ string à¸à¸µà¹à¸¡à¸µà¹à¸à¸£à¸·à¹à¸à¸à¸«à¸¡à¸²à¸¢ comma</strong></p>
<pre class="lang:default decode:true " >addComma(1400); //à¸à¸¥à¸¥à¸±à¸à¸à¹ = 1,400</pre>
<p></br></p>
<h4>à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸ªà¸¸à¹à¸¡à¹à¸à¹à¸à¸ªà¸µà¹à¸à¸ hex</h4>
<pre class="lang:default decode:true " >function randomColor() { 
 var letters = '0123456789ABCDEF'.split(''); 
 var color = '#'; 
 for (var i = 0; i <; 6; i++ ) { 
 color += letters[Math.floor(Math.random() * 16)]; 
 } 
 return color; 
}</pre>
<p><strong>à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸</strong></p>
<pre class="lang:default decode:true " >randomColor(); // à¸à¸¥à¸¥à¸±à¸à¸à¹ = #650DE6</pre>
<p></br></p>
<h4>à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸ªà¸¸à¹à¸¡à¹à¸à¹à¸à¸£à¸«à¸±à¸ª</h4>
<pre class="lang:default decode:true " >function randomCode() { 
 var code = ""; 
 var possible = "abcdefghijklmnopqrstuvwxyz0123456789"; 
 for( var i=0; i <; 10; i++ ) 
 code += possible.charAt(Math.floor(Math.random() * possible.length)); 
 
 return code; 
}</pre>
<p><strong>à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸</strong></p>
<pre class="lang:default decode:true " >randomCode(); // à¸à¸¥à¸¥à¸±à¸à¸à¹ = r7nd4l7e86</pre>
<p></br></p>
<h4>à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¹à¸à¸¥à¸à¸à¹à¸²à¸ªà¸µà¸à¸²à¸ RGB à¹à¸à¹à¸ Hex</h4>
<pre class="lang:default decode:true " >function RGBtoHEX(r,g,b) 
{ 
	componentToHex = function(c) { 
		var hex = c.toString(16); 
		return hex.length == 1 ? "0" + hex : hex; 
	} 
	return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b); 
} 
</pre>
<p><strong>à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸</strong></p>
<pre class="lang:default decode:true " >RGBtoHEX(255,255,255); // à¸à¸¥à¸¥à¸±à¸à¸à¹ = #ffffff</pre>
<p></br></p>
<h4>à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¹à¸à¸¥à¸à¸à¹à¸²à¸ªà¸µà¸à¸²à¸ Hex à¹à¸à¹à¸ RGB</h4>
<pre class="lang:default decode:true " >function hexToRGB(hex) { 
 var r = parseInt((cutHex(hex)).substring(0,2),16); 
	var g = parseInt((cutHex(hex)).substring(2,4),16); 
	var b = parseInt((cutHex(hex)).substring(4,6),16); 
	function cutHex(hex) {return (hex.charAt(0)=="#") ? hex.substring(1,7):hex} 
 return r + "," + g + "," + b; 
}</pre>
<p><strong>à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸</strong></p>
<pre class="lang:default decode:true " > hexToRGB('#ffffff'); // à¸à¸¥à¸¥à¸±à¸à¸à¹ = 255,255,255</pre>
<p></br></p>
<h4>à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¸·à¸à¸à¹à¸²à¸à¸¨à¸à¸´à¸¢à¸¡ 2 à¸à¸³à¹à¸«à¸à¹à¸</h4>
<pre class="lang:default decode:true " >function fixDecimal(num){ 
	return parseFloat(Math.round(num * 100) / 100).toFixed(2);	 
}</pre>
<p><strong>à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸</strong></p>
<pre class="lang:default decode:true " >fixDecimal(24.5600); // à¸à¸¥à¸¥à¸±à¸à¸à¹ = 24.56</pre>
<p></br></p>
<h4>à¸à¸±à¸à¸à¹à¸à¸±à¹à¸à¸à¸£à¸§à¸à¸ªà¸à¸à¸à¸²à¸£à¹à¸à¹à¸²à¸à¸¡à¹à¸§à¹à¸à¹à¸à¸à¹à¸à¹à¸²à¸à¸¡à¸·à¸à¸à¸·à¸</h4>
<pre class="lang:default decode:true " >function isMobile() { 
	 if( navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i) 
	 ){ return true; }else{ return false; } 
}</pre>
<p><strong>à¸à¸±à¸§à¸à¸¢à¹à¸²à¸à¸à¸²à¸£à¹à¸à¹à¸à¸²à¸ </strong></p>
<pre class="lang:default decode:true " >isMobile(); //à¸à¸¥à¸¥à¸±à¸à¸à¹ = false</pre>
<p></br></p>

รวม javascript utils ฟังก์ชั่นอรรถประโยชน์ที่ใช้งานบ่อย

รวมฟังก์ชั่น-javascript-utils
