Loading
2009. 12. 29. 14:43 - 나쁜철군

ASP Server.HTMLEncode , HTMLDecode


▶ HTMLEncode
HTML Source를 브라우저에서 그대로 볼 수 있게 변환해 주는 메소드 입니다.
HTML tag는 <>로 구분하는 데, 우리가 HTML 태그를 브라우저에서 그대로 보고자 하면 <기호는 < 로 >기호는 >로 변환해 주면 HTML Tag를 볼 수 있습니다.

구문
Server.HTMLEncode( string )


매개 변수 : string 
인코딩할 문자열을 지정합니다.

예제
예를 들어 화면에 <p> 이라고 표시하려면, HTML Tag 상에서는 <p> 와 같이 작성해야 한다는 것을 알고 계실 겁니다. 하지만, 다음 스크립트처럼 표현할 수도 있습니다.


<%= Server.HTMLEncode("The paragraph tag: <p>") %>

위 예는 다음과 같은 출력을 작성하며, -> The paragraph tag: <P>

웹 브라우저에서는 다음과 같이 나타납니다. -> The paragraph tag: <P>

그리고, ASP태그 (<%...%>태그)를 화면에 나타내기 위해서는 다음처럼 \를 추가하여야 합니다. 그렇지 않으면 에러가 발생합니다.

Response.Write Server.HTMLEncode("<%= Server.ScriptTimeout %>") -> 에러발생
Response.Write Server.HTMLEncode("<%= Server.ScriptTimeout %\>") -> <%= Server.ScriptTimeout %> 출력



HTMLDecode

Just like with the URLDecode function described previously, Microsoft, in its infinite wisdom decided not to include an HTMLDecode function with their Server component. It is a relatively simple matter to decode this test data (although I haven't had a need to do this so far.) For completeness sake, here is an HTMLDecode function you may use:

Function HTMLDecode(sText)
    Dim I
    sText = Replace(sText, "&quot;", Chr(34))
    sText = Replace(sText, "&lt;"  , Chr(60))
    sText = Replace(sText, "&gt;"  , Chr(62))
    sText = Replace(sText, "&amp;" , Chr(38))
    sText = Replace(sText, "&nbsp;", Chr(32))
    For I = 1 to 255
        sText = Replace(sText, "&#" & I & ";", Chr(I))
    Next
    HTMLDecode = sText
End Function

  HTMLDecode는 게시판 같은 경우 HTML로 작성했을경우에  TEXT로 저장된 내용을 HTML로 보여주는 역활