[Javascript] Function 모음.
//백스페이스 방지. 2015.10.20 추가. $(document).keydown(function(e){ if(e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA" ){ if(e.keyCode === 8){ return false ; } } }); |
/* FireFox는 window.event가 없어서 추가 */ if($.browser.mozilla){ $.each(["mousedown", "mouseover", "mouseout", "mousemove", "mousedrag", "click", "dblclick"], function(i, eventName){ window.addEventListener(eventName, function (e){ window.event = e; }); }); } |
function isIE() { if( navigator.userAgent.indexOf( "MSIE")>=0 || navigator.userAgent.indexOf( "Trident")>=0) return true ; else return false ; } |
/** * Escape 문자 -> HTML * .replace(/&/g, '&').replace(/>/g, '>').replace(/< /g, '<').replace(/\n/g, '<br>' ).replace(/ /g, ' ')); */ var entityMap = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'", "/": "/", " ": " " };
function escapeHtml(string) { return String(string).replace(/[& <>"'\/]/g, function (s) { return entityMap[s]; }); } |
//yyyymmdd 형태로 포매팅된 날짜 반환 Date.prototype.yyyymmdd = function() { var yyyy = this.getFullYear().toString(); var mm = ( this.getMonth() + 1).toString(); var dd = this.getDate().toString();
return yyyy + (mm[1] ? mm : '0'+mm[0]) + (dd[1] ? dd : '0' +dd[0]); }
console.log((new Date()).yyyymmdd()); |
/** * replaceAll 구현 */ function replaceAll(sValue, param1, param2) { return sValue.split(param1).join(param2); }
// replaceAll prototype 추가 String.prototype.replaceAll = function () { var a = arguments, length = a.length; if ( length == 0 ) { return this ; } var regExp = new RegExp( a[0], "g" ); if ( length == 1 ) { return this .replace(regExp, ""); } else { return this .replace(regExp, a[1]); } return this ; }; |