사이트 내 전체검색
escape(), encodeURI(), encodeURIComponent() 차이
로빈아빠
https://cmd.kr/javascript/756 URL이 복사되었습니다.

본문

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

댓글목록

등록된 댓글이 없습니다.

831 (4/17P)

Search

Copyright © Cmd 명령어 18.119.157.62