'Error Handling'에 해당되는 글 1

  1. 2006.07.21 프로시져를 사용하여 자바스크립트 에러 메시지 출력

프로시져를 사용하여 자바스크립트 에러 메시지 출력

Written by Ssemi™(semin Seol), www.Ssemi.net

Request개체로 넘어온 Form-Data 쉽게 알아내기에 이어진 라이브러리 2탄!!
웹개발을 하다보면 서버스크립트인 ASP 이외에도 클라이언트 스크립트인 Javascript의 사용은 필수입니다.

아래와 같은 코드를 보십시오.

<%
   '작업 끝! - 커넥션 풀링
   IF oCn.State = 1 Then oCn.Close
%>
<script type="text/javascript">
<!--
   window.alert('확인 테스트');
   document.location.href = '/test.asp';
// -->
</script>
<%
   Response.End
%>
위와 같이 서버/클라이언트의 구분이 있을 경우에는 코딩이 지저분해지고, 중간중간마다 <% %> 구분을 해주면서 Javascript도 함께 써야 하는 불편함이 있습니다.
바로 이와 같은 코드의 불편함을 줄여주고자 프로시져를 사용하여 에러 메시지를 출력합니다.

아래는 프로시져를 사용하여 변경한 코드입니다.
<%
   '작업 끝! - 커넥션 풀링
   IF oCn.State = 1 Then oCn.Close

   FlushError "확인 테스트", "Location", null, "/test.asp"
%>
더욱 깔끔한 코드로 만들 수 있습니다. 가독성이 더욱 뛰어나집니다. ^_^

물론 이와 같은 코드를 모두들 사용하고 계실 것이라 생각합니다.
하지만 선물세트 같이 다용도로 쓰일 수 있게 만든 프로시져라 공개해봅니다 :)


FlushError Procedure

이 프로시져는 서버/클라이언트간에 Javascript 에러 메시지 사용에 있어서 유연한 코딩을 제공해줍니다.
프로시져를 사용함으로서 얻어지는 가독성과 편리함을 느껴보세요 ^_^

▶ 사용법

FlushError 프로시져에는 4가지의 인자(Argument)가 존재합니다.
  • ErrorText : 에러메시지 출력
    - 인자의 값이 Null 이거나 길이가 0 일때는 에러메시지가 나오지 않음

  • nOption : 에러메시지 이후의 행동을 명령
    - Default Value : Default // history.back() or document.location.href 의 기능을 수행
    - Back // history.go(nGo)을 통해 history 이동 가능(일반적으로 history.back()의 기능의 명령)
    - Location // document.location.href 를 통해 페이지 이동을 위한 명령
    - Close // 현재 창을 닫고 싶을 때 사용
    - Opener // 부모창을 이동시키고, 현재 창을 닫는데 사용
    - Stop // 아무 반응 없음

  • nGo : history.go(#) 안의 인수(#)
    - Default Value : -1 // history.back()과 같은 기능

  • nPlace : Location.href 를 통해 페이지 이동시에 이동할 위치를 지정
    - Default Value : / // Rootpage

▶ 코드

Sub FlushError(ErrorText, nOption, nGo, nPlace) 
   Response.Write "<script type=""text/javascript"">" & chr(13)
   Response.Write "<!-- " & chr(13)
   
   IF Not isNull(ErrorText) And Len(ErrorText) > 0 Then
       Response.Write "window.alert('"& ErrorText &"');" & chr(13)
   End IF

   IF isNull(nOption) OR Len(nOption) = 0 Then nOption = "default"
   IF isNull(nGo) OR Not isNumeric(nGo) OR Len(nGo) > 2 OR nGo = 0 Then nGo = "-1"
   IF isNull(nPlace) OR Len(nPlace) = 0 OR isNumeric(nPlace) Then nPlace = "/"

   Select Case LCase(CSTR(nOption))
       Case "back"
           Response.Write "history.go("& nGo &");" & chr(13)

       Case "location"
           Response.Write "document.location.href='"& nPlace &"';" & chr(13)

       Case "close"
           Response.Write "self.opener = self;" & chr(13)
           Response.Write "self.close();" & chr(13)

       Case "opener"
           Response.Write "if (window.opener && window.opener != null) { " & chr(13)
           Response.Write "window.opener.location.href='"& nPlace &"';" & chr(13)
           Response.Write "window.close();" & chr(13)
           Response.Write "} else { " & chr(13)
           Response.Write "window.close();" & chr(13)
           Response.Write "}" & chr(13)

       Case "default"
           Response.Write "if (history.length>0) history.go("& nGo &");" & chr(13)
           Response.Write "else document.location.href='"& nPlace &"';" & chr(13)
       Case Else

   End Select

   Response.Write "// --> " & chr(13)    
   Response.Write "</script>" & chr(13)
   Response.End
End Sub

▶ 예제코드

' 테스트1 이라는 에러메시지 출력 후 history.back()기능 수행
FlushError "테스트1", "back", -1, 1

' 테스트2 이라는 에러메시지 출력 후 /test.asp 로 이동
FlushError "테스트2", "location", -1, "/test.asp"

' 테스트3 이라는 에러메시지 출력 후 현재 창을 닫음
FlushError "테스트3", "close", -1, 1

' 테스트4 이라는 에러메시지 출력 후 부모창을 /test.asp 로 옮긴 후 현재 창을 닫음
FlushError "테스트4", "opener", -1, "/test.asp"

' 테스트5 이라는 에러메시지 출력 후 멈춤
FlushError "테스트5", "stop", -1, 1

' 에러메시지 없이 /test.asp 로 이동
FlushError "", "Location", -1, "/test.asp"
* nGo, nPlcae 인자는 nOption 에 따라 쓰일 수도, 쓰이지 않을 수도 있음에 유의하여
실제 코딩시에는 FlushError "테스트", "stop", null, null 이라는 명령이 아닌
FlushError "테스트5", "stop", -1, 1 로 쓰게 되면 훨씬 코딩의 이로움이 생깁니다.

도움이 되었길 바라면서 이만 ^_^
2006. 7. 21. 08:29