자바스크립트 파일명명법은 java,jsp 파일명과 동일하게 camel 표기법으로 명명한다.
주의사항 : - 다른 파일냉의 동일한 메쏘드명으로 작성하지 않는다. - js 파일은 항상 인코딩 타입을 euc-kr (ms949 ascii)타입으로 코딩하되 파일자체가 Ajax를 통해 불러오는 페이지의 경우는 반드시 UTF-8 형태로 작성한다. (<script type="text/javascript" src="흠.js" charset="euc-kr"></script>) - 각 비즈니스(여기서는 메뉴)별 스크립트를 나누어 작성하되 필요한 경우에만 불러 사용한다. - 공통 클래스는 클래스임을 표기하여 준다. (sortTableClass.js) - 해당 비즈니스(여기서는 메뉴)에만 전용으로 쓰는 스크립트가 많다면 비즈니스명으로 폴더링한다.
표준 자바스크립트 사용
객체 접근은 cross-browser를 지원하는 표준 메쏘드나 프로퍼티를 사용한다. 1. document.all 보다는 document.getElementById, document.getElementsByName 등으로 접근한다. 2. eval은 되도록이면 자제하도록한다. 3. 객체가 create되면 반드시 destory할때 null처리를 한다. 4. try~ catch~throws 문은 필요한 부분에서만 사용한다.
IE5에서 첫선을 보인 객체.getBoundingClientRect는 객체의 offset의 top, left,width,height를 반환하는 멋진놈이다. 일반적으로 gecko 엔진을 사용하는 FF2에서는 getBoxObjectFor(객체)를 사용했으나 FF3에서는 getBoxObjectFor()가 없어지고 getBoundingClientRect가 생겨났다.
FF3가 출시되자 마자 기존 스크립트들을 많이 바꾸고 있는데 이중에 getBoundingClientRect가 포함되었다. 드디어 FF도 IE의 손을 들어준것인가? IE8에서는 얼마만큼 오픈소스진영의 손을 들어줄것인지 귀추가 주목된다.
데꾸벅이 사용하는 객체의 offset left,top,width, height반환하는 함수
/** * tag 객체의 위치값 및 너비/높이값을 반환한다. * @param {objId} DOM객체 : document.getElementById() * @return {ret} left,top,width,height 를 반환한다. * @author 데꾸벅 */ function getBoundsObject(objId){ var techbug = new Object(); var tag = document.getElementById(objId);
if(tag !=null && tag != undefined ){ if(tag.getBoundingClientRect){ //IE, FF3 var rect = tag.getBoundingClientRect(); techbug.left = rect.left + (document.documentElement.scrollLeft || document.body.scrollLeft); techbug.top = rect.top + (document.documentElement.scrollTop || document.body.scrollTop); techbug.width = rect.right - rect.left; techbug.height = rect.bottom - rect.top +1; // +1 = Moz와 맞춤 } else if (document.getBoxObjectFor) { // gecko 엔진 기반 FF3제외 var box = document.getBoxObjectFor(tag); techbug.left = box.x; techbug.top = box.y; techbug.width = box.width; techbug.height = box.height; }else { techbug.left = tag.offsetLeft; techbug.top = tag.offsetTop; techbug.width = tag.offsetWidth; techbug.height = tag.offsetHeight + 3; // +1 = Moz와 맞춤 var parent = tag.offsetParent; if (parent != tag) { while (parent) { techbug.left += parent.offsetLeft; techbug.top += parent.offsetTop; parent = parent.offsetParent; } } // 오페라와 사파리의 'absolute' postion의 경우 body의 offsetTop을 잘못 계산 보정 var ua = navigator.userAgent.toLowerCase(); if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && getStyle(tag, 'position') == 'absolute' )) { techbug.top -= document.body.offsetTop; }