블로그 이미지
좋은느낌/원철
이것저것 필요한 것을 모아보렵니다.. 방문해 주셔서 감사합니다..

calendar

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Notice

    2009. 2. 24. 15:22 개발/JavaScript

    JAVA와 Javascript간에 Ajax로 통신을 하다 보니 인코딩상에 문제가 발생하는것을 알게 되었다.

    물론 encodeURIComponent 같은것을 이용했지만 특수문자등이 정확히 인코딩/디코딩 되지 않는 문제가 있었다.

    다음의 유틸을 사용하니깐 잘 되었다.

    /*
     문자열을 인코딩 할때 사용한다. 다음과 같이 디코딩 하여 사용한다.
     JAVA : URLEncoder.decode(str, "UTF-8")
     JS : decodeURL(str)
    */
    function encodeURL(str){
        var s0, i, s, u;
        s0 = "";                // encoded str
        for (i = 0; i < str.length; i++){   // scan the source
            s = str.charAt(i);
            u = str.charCodeAt(i);          // get unicode of the char
            if (s == " "){s0 += "+";}       // SP should be converted to "+"
            else {
                if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){       // check for escape
                    s0 = s0 + s;            // don't escape
                }
                else {                  // escape
                    if ((u >= 0x0) && (u <= 0x7f)){     // single byte format
                        s = "0"+u.toString(16);
                        s0 += "%"+ s.substr(s.length-2);
                    }
                    else if (u > 0x1fffff){     // quaternary byte format (extended)
                        s0 += "%" + (0xf0 + ((u & 0x1c0000) >> 18)).toString(16);
                        s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
                        s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                        s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                    }
                    else if (u > 0x7ff){        // triple byte format
                        s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
                        s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                        s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                    }
                    else {                      // double byte format
                        s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
                        s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                    }
                }
            }
        }
    

        return s0;

    }

    /* 문자열을 디코딩 할때 사용한다. 다음과 같이 인코딩 하여 사용한다. JAVA : URLEncoder.encode(str, "UTF-8") JS : encodeURL(str) */ function decodeURL(str) {     var s0, i, j, s, ss, u, n, f;     s0 = "";                // decoded str     for (i = 0; i < str.length; i++){   // scan the source str         s = str.charAt(i);         if (s == "+"){s0 += " ";}       // "+" should be changed to SP         else {             if (s != "%"){s0 += s;}     // add an unescaped char             else{               // escape sequence decoding                 u = 0;          // unicode of the character                 f = 1;          // escape flag, zero means end of this sequence                 while (true) {                     ss = "";        // local str to parse as int                         for (j = 0; j < 2; j++ ) {  // get two maximum hex characters for parse                             sss = str.charAt(++i);                             if (((sss >= "0") && (sss <= "9")) || ((sss >= "a") && (sss <= "f"))  || ((sss >= "A") && (sss <= "F"))) {                                 ss += sss;      // if hex, add the hex character                             } else {--i; break;}    // not a hex char., exit the loop                         }                     n = parseInt(ss, 16);           // parse the hex str as byte                     if (n <= 0x7f){u = n; f = 1;}   // single byte format                     if ((n >= 0xc0) && (n <= 0xdf)){u = n & 0x1f; f = 2;}   // double byte format                     if ((n >= 0xe0) && (n <= 0xef)){u = n & 0x0f; f = 3;}   // triple byte format                     if ((n >= 0xf0) && (n <= 0xf7)){u = n & 0x07; f = 4;}   // quaternary byte format (extended)                     if ((n >= 0x80) && (n <= 0xbf)){u = (u << 6) + (n & 0x3f); --f;}         // not a first, shift and add 6 lower bits                     if (f <= 1){break;}         // end of the utf byte sequence                     if (str.charAt(i + 1) == "%"){ i++ ;}                   // test for the next shift byte                     else {break;}                   // abnormal, format error                 }             s0 += String.fromCharCode(u);           // add the escaped character             }         }     }     return s0;

    }

    posted by 좋은느낌/원철
    2008. 10. 25. 20:12 개발/JavaScript

    자바스크립트 날짜 계산 함수

    <script language="JavaScript">
    <!--
    /**
     param date : Date Objeet
     return string "YYYYMMDD"
     usage : getDateObjToStr(new Date());
    */
    function getDateObjToStr(date){
     var str = new Array();

     var _year = date.getFullYear();
     str[str.length] = _year;
     
     var _month = date.getMonth()+1;
     if(_month < 10) _month = "0"+_month;
     str[str.length] = _month;
     
     var _day = date.getDate();
     if(_day < 10) _day = "0"+_day;
     str[str.length] = _day
     var getDateObjToStr = str.join("");

     return getDateObjToStr;
    }

    /**
     getDateObjToStr 함수 필요
     return Today "YYYYMMDD"
    */
    function getToday(){
     var d = new Date();
     var getToday = getDateObjToStr(d);
     return getToday;
    }

    /**
     데이트 계산 함수
     param date : string "yyyymmdd"
     param period : int
     param period_kind : string "Y","M","D"
     param gt_today : boolean
     usage : calcDate("20080205",30,"D");
    */
    function calcDate(date,period, period_kind,gt_today){

     var today = getToday();

     var in_year = date.substr(0,4);
     var in_month = date.substr(4,2);
     var in_day = date.substr(6,2);
     
     var nd = new Date(in_year, in_month-1, in_day);
     if(period_kind == "D"){
      nd.setDate(nd.getDate()+period);
     }
     if(period_kind == "M"){
      nd.setMonth(nd.getMonth()+period);
     }
     if(period_kind == "Y"){
      nd.setFullYear(nd.getFullYear()+period);
     }
     var new_date = new Date(nd);
     var calcDate = getDateObjToStr(new_date);
     if(gt_today){ // 금일보다 큰 날짜 반환한다면
      if(calcDate > today){
       calcDate = today;
      }
     }
     return calcDate;
    }
    //-->
    </script>

    posted by 좋은느낌/원철
    2008. 9. 2. 00:43 개발/JavaScript
    출처 : http://web20korea.com/205

    javascript :
    function FN_iframeAutoHeight(obj) {
     obj = typeof obj == 'string' ? document.getElementById(obj) : obj;
     obj.setExpression('height',iframename.document.body.scrollHeight);
    }
    html code :
    <IFRAME name="iframename" border=0 marginWidth=0 marginHeight=0 src="http://web20korea.com" frameBorder=0 width="100%" scrolling=no onload="FN_iframeAutoHeight(this)"></IFRAME>

    posted by 좋은느낌/원철
    2008. 8. 23. 11:52 개발/JavaScript

    출처 : OKJSP
    원문 : http://www.okjsp.pe.kr/seq/103876

    <!--
    부모 페이지
    -->

    <script>
    function clickMove2(){
        window.open('',"sm",'width=1024,height=768,left=0,top=0,toolbar=no,location=no,menubar=no,status=no,directories=no,resizable=yes,scrollbars=yes');

     document.frames["tfrm"].listForm.target = "sm";

     document.frames["tfrm"].listForm.action = "detail_view.jsp";
        document.frames["tfrm"].listForm.submit();
    }
    </script>


    <iframe src='%s' name='tfrm' width='100%' height='400' frameborder='0'></iframe>

    <a href="javascript:clickMove2();">링크</a>


    <!--
    iFrame
    -->

    <form name='listForm' method='post'>
    <table>
    </table>
    </form>

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    난 팝업으로 띄우지 않고 그냥 삽입할 예정이다..
    잘 되어야 할 텐데..

    posted by 좋은느낌/원철
    prev 1 next