자바스크립트로 쿼리스트링 값 받아오기...흠.

function GetParameter(sName)
{

 var sUrl = location.href;
 var nStartPoint = sUrl.indexOf("?");
 var sQueryStr = sUrl.substring(nStartPoint + 1, sUrl.length);
 var arrName = new Array();
 var arrValue = new Array();
 var nNextStartPoint = 0;
 var sItem;
 var nValuePoint;
 
 while (sQueryStr.indexOf("&") > -1)
 {

  nNextStartPoint = sQueryStr.indexOf("&")
  sItem = sQueryStr.substring(0, nNextStartPoint);
  nValuePoint = sItem.indexOf("=");
  arrName[arrName.length] = sItem.substring(0, nValuePoint);
  arrValue[arrValue.length] = sItem.substring(nValuePoint+1, sItem.length);
  sQueryStr = sQueryStr.substring(nNextStartPoint+1, sQueryStr.length);

 }
 
 sItem = sQueryStr;
 nValuePoint = sItem.indexOf("=");
 arrName[arrName.length] = sItem.substring(0, nValuePoint);
 arrValue[arrValue.length] = sItem.substring(nValuePoint+1, sItem.length);
 
 var sValue = "";
 for(var cnt = 0; cnt < arrName.length; cnt++)
 {
       if (arrName[cnt] == sName)
      {
                sValue = arrValue[cnt];
      }
 
 }
 
 return sValue;

}

 예> ..../test.html?params=1234 

GetParameter("params");     // 결과 : 1234

반응형

+ Recent posts