Skip to content

escape(), encodeURI(), encodeURIComponent() 차이

MintState February 13, 2009 at 2:06 PM Views 19357

escape(), encodeURI(), encodeURIComponent() 차이

비슷하면서도 다른 함수들 입니다.
세가지 함수 모두 ASCII문자로 바꿔주는데, 그럼 각각의 차이점을 알아 보겠습니다.

1. escape() : unescape()

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
1234567890
@*-_+./

위에서 열거된 문자가 아니면 모두 변환을 합니다. 1바이트문자는 %XX 형태로 2바이트 문자는 %uXXXX 식으로 변환합니다.

2. encodeURI() : decodeURI()
escape()와 같이 변환을 하지만, 인터넷 주소에서 쓰는 특수 문자 : ; / = ? & 는 변환을 하지 않습니다.

3. encodeURIComponent() : decodeURIComponent()
인터넷 주소에서 쓰는 특수 문자 : ; / = ? & 까지 변환을 합니다. 인터넷 주소를 하나의 변수에 넣을때 쓸 수 있습니다.

예제)

<script type="text/javascript">
var url = "http://yesyo.com/?retrun=고고";
document.write('<p><b>URL :</b> ' + url + '</p>');
document.write('<p><b>escape() :</b> ' + escape(url) + '</p>');
document.write('<p><b>encodeURI() :</b> ' + encodeURI(url) + '</p>');
document.write('<p><b>encodeURIComponent() :</b> ' + encodeURIComponent(url) + '</p>');
</script>

결과 :
URL : http://yesyo.com/?retrun=고고
escape() : http%3A//yesyo.com/%3Fretrun%3D%uACE0%uACE0
encodeURI() : http://yesyo.com/?retrun=%EA%B3%A0%EA%B3%A0
encodeURIComponent() : http%3A%2F%2Fyesyo.com%2F%3Fretrun%3D%EA%B3%A0%EA%B3%A0

Share X Facebook LINE QR code

0 comments

Be the first to comment.