/** * apply method 사용 * 이벤트발생시 함수에 전달할때 클래스안의 this 를 포함한다. */ Function.prototype.bind = function() { for ( var i = 0, method = this , args = [] , len = arguments.length ; i < len ; i++ ) { args.push( arguments[ i ] ); } return function() { return method.apply( args[0] , args.slice(1) ); } } function debug(o, options) { // check if( o == null ) return; if( typeof options == 'undefined' ) options = {pos: false}; // process if( typeof o == 'string' || typeof o == 'number' ) { var debugStr = o; } else { // process #2 var debugStr = ''; for( i in o ) { //if( o[i] == '[object HTMLFormElement]' ) continue; debugStr += ''; if( options.optSub == 1 ) { if( typeof o[i] == 'object' ) { for( j in o[i] ) { debugStr += '======>' + j + '=' + o[i][j] + '
'; } } } } debugStr += '
' + i + '' + o[i] + '' + typeof o[i] + '
'; if( options.cookie == true ) debugStr += 'document.cookie:' + document.cookie + '
'; } var divObj = document.createElement('div'); divObj.id = 'debug'; if( options.pos == true ) { divObj.style.position = 'absolute'; divObj.style.top = ( options.top || 0 ) + 'px'; divObj.style.left = ( options.left || 500 ) + 'px'; divObj.style.zIndex = options.zIndex || 10000; } divObj.innerHTML = debugStr; document.body.appendChild(divObj); } /************************************* * create XMLHTTPRequest Object * xhr ( xml http request ) * ex1) usage var o = xhr.init(); *************************************/ var xhr = { instance: false, init: function() { if( typeof XMLHttpRequest != 'undefined' ) { this.instance = new XMLHttpRequest(); } if( !this.instance ) { try { this.instance = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { this.instance = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { this.instance = false; } } } return this.instance; } } /** * 오브젝트 확장 * @param {Object} destination * @param {Object} source * @return {Object} */ Object.extend = function(destination, source) { for(var property in source) { destination[property] = source[property]; } return destination; } /** * Ajax */ var Ajaxs = { Events: ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'], // readyState 에 대한 이벤트명 setOptions: { method: 'post', asynchronous: true, contentType: 'application/x-www-form-urlencoded', encoding: 'UTF-8', parameters: '' }, // 기본 옵션 /** * 실행함수 * @param {String} url * @param {Object object} options 옵션 JSON */ init: function(url, options) { this.url = url; this.xhrObj = xhr.init(); Object.extend(this.setOptions, options || {}); this.request(); }, /** * 요청 */ request: function() { // when GET, append parameters to URL //var params = encodeURIComponent(this.setOptions.parameters); var params = this.setOptions.parameters; if( this.setOptions.method == 'get' && params ) this.url += (this.url.indexOf('?') > -1 ? '&' : '?') + params; try { if(typeof this.onCreate == 'function') this.onCreate(); // 최초함수 실행 this.xhrObj.open(this.setOptions.method, this.url, this.setOptions.asynchronous); this.xhrObj.onreadystatechange = this.onStateChange.bind(this); this.header(); var body = this.setOptions.method == 'post' ? (this.setOptions.postBody || params) : null; this.xhrObj.send(body); /* Force Firefox to handle ready state for synchronous requests */ if( !this.setOptions.asynchronous && this.xhrObj.overrideMimeType ) this.onStateChange(); } catch(e) { debug(e); } }, /** * 헤더선언 */ header: function() { var headers = {}; headers['X-Requested-With'] = 'XMLHttpRequest'; //ajax 요청여부 heade값 추가 if(this.setOptions.method == 'post') { headers['Content-Type'] = this.setOptions.contentType + ( this.setOptions.encoding ? '; charset=' + this.setOptions.encoding : '' ); } for(var name in headers) this.xhrObj.setRequestHeader(name, headers[name]); }, /** * monitor */ onStateChange: function() { //if( this.xhrObj.readyState > 1 && !( this.xhrObj.readyState == 4 && this._complete ) ) if( this.xhrObj.readyState > 1 ) this.respond(this.xhrObj.readyState); }, /** * 요청 완료 */ success: function() { return !this.xhrObj.status || (this.xhrObj.status >= 200 && this.xhrObj.status < 300); }, /** * 요청상태별 체크 * @param {String} readyState */ respond: function(readyState) { var state = this.Events[readyState]; if( state == 'Complete' ) { this._complete = true; var json = this.evalJSON(); if( typeof this.setOptions.onComplete == 'function' ) { this.setOptions.onComplete(this.xhrObj, json); } } }, /** * 요청한 URL 에 대한 헤더얻음 * @param {String} name 헤더 구분값 * @return {Boolen} true/false */ getHeader: function(name) { try { return this.xhrObj.getResponseHeader(name); } catch(e) { debug(e); } return true; }, /** * JSON 값을 가져옴 */ // return json type evalJSON: function() { try { var json = this.getHeader('X-JSON'); return json ? eval('(' + json + ')') : null; } catch (e) { return null } } } //폼 시리얼라이즈 var serialize = { getSerialize : function(form) { //alert(form); var formElements = form.getElementsByTagName('*'); var s = []; var key = value = null; for(var i= 0; i < formElements.length; i++ ) { key = formElements[i].name; value = serialize.getValue(formElements[i]); if(value != null) { switch(formElements[i].type) { case 'file': case 'submit': case 'button': case 'reset': break; default: s.push(formElements[i].name + "=" + encodeURIComponent(value)); break; } } } return s.join("&"); }, getValue : function(element) { var tag = element.tagName.toLowerCase(); switch(tag) { case 'input': switch(element.type.toLowerCase()) { case 'checkbox': case 'radio': return (element.checked==true) ? element.value : null; break; default: return element.value; break; } break; case 'textarea': return element.value; break; case 'select': var index = element.selectedIndex; return index >= 0 ? element.options[index].value : null; break; default: return null; break; }//ens switch } };