'예제'에 해당되는 글 1건

  1. 2009.08.04 Ajax 예제코드? 1

Ajax 예제코드?

Javascript 2009. 8. 4. 21:50

var productGridDiv;
var please_wait = "<img src='/us/p2images/common/loading.gif' />";

function chkAjaBrowser()
{
 var a,ua = navigator.userAgent;
 this.bw= {
   safari    : ((a=ua.split('AppleWebKit/')[1])?a.split('(')[0]:0)>=124 ,
   konqueror : ((a=ua.split('Konqueror/')[1])?a.split(';')[0]:0)>=3.3 ,
   mozes     : ((a=ua.split('Gecko/')[1])?a.split(" ")[0]:0) >= 20011128 ,
   opera     : (!!window.opera) && ((typeof XMLHttpRequest)=='function') ,
   msie      : (!!window.ActiveXObject)?(!!createHttpRequest()):false
 }
 return (this.bw.safari||this.bw.konqueror||this.bw.mozes||this.bw.opera||this.bw.msie)
}

function createHttpRequest()
{
 if(window.ActiveXObject){
  try {
   return new ActiveXObject("Msxml2.XMLHTTP") ;
  } catch (e) {
   try {
    return new ActiveXObject("Microsoft.XMLHTTP") ;
   } catch (e2) {
    return null ;
    }
   }
 } else if(window.XMLHttpRequest){
  return new XMLHttpRequest() ;
 } else {
  return null ;
 }
}

function sendRequest(callback,data,method,url,async,sload)
{
 var oj = createHttpRequest();
 if( oj == null ) return null;
 
 var sload = (!!sendRequest.arguments[5])?sload:false;
 if(sload || method.toUpperCase() == 'GET')url += "?";
 if(sload)url=url+"t="+(new Date()).getTime();
 
 var bwoj = new chkAjaBrowser();
 var opera   = bwoj.bw.opera;
 var safari   = bwoj.bw.safari;
 var konqueror = bwoj.bw.konqueror;
 var mozes   = bwoj.bw.mozes ;

 if(opera || safari || mozes){
  oj.onload = function () { callback(oj); }
 } else {
 
  oj.onreadystatechange =function ()
  {
   if ( oj.readyState == 4 ){
    callback(oj);
   }
  }
 }

 data = uriEncode(data)
 if(method.toUpperCase() == 'GET') {
  url += data
 }

 oj.open(method,url,async);
 
 setEncHeader(oj)

 oj.send(data);

 function setEncHeader(oj){

  var contentTypeUrlenc = 'application/x-www-form-urlencoded; charset=UTF-8';
  
  if(!window.opera){
   oj.setRequestHeader('Content-Type',contentTypeUrlenc);
  } else {
   if((typeof oj.setRequestHeader) == 'function')
    oj.setRequestHeader('Content-Type',contentTypeUrlenc);
  }
  
  return oj
 }

 return oj
}

function response(req) {
 if (req.readyState != 4)
  return;
 var tmp;
 if (req.status == 200 || req.status == 0) {
  tmp = req.responseText
 }
 
 if(tmp == 'error'){
  document.location = "/us/common/notfound.html";
 }
 document.getElementById('subtype_transparency').style.display='none';
 document.getElementById('subtype_transparency_image').style.display='none';
 productGridDiv.innerHTML = tmp;
 compare_initialize ();
}

function uriEncode(data){

 if(data!=""){
  var encdata = '';
  var datas = data.split('&');
  for(i=1;i<datas.length;i++)
  {
   var dataq = datas[i].split('=');
   encdata += '&' + encodeURIComponent(dataq[0])+'='+encodeURIComponent(dataq[1]);
  }
 } else {
  encdata = "";
 }
 return encdata;
}

function open_url(url, params, targetId) {
 productGridDiv = document.getElementById(targetId)
 
// productGridDiv.innerHTML = please_wait;
// document.getElementById("subtype_transparency").style.height='2000px';
 document.getElementById('subtype_transparency').style.display='block';
 document.getElementById('subtype_transparency_image').style.display='block';

 req = sendRequest(response, params, "GET", url, true);

}



※ open_url() 함수의 콜을 시작으로 (url, params, targetId) 데이터를 갱신하여 targetId value의 레이어에 전송.

제작자 : 프리랜서 이현영 과장님....

오픈소스니까 저작권 신경안씀.

현재 작업하게 된 DockUI <-> 연동해서 작업할 계획.

어느정도 튜닝필요.

Posted by 철냄비짱
,