encodeURI VS encodeURIComponent (javascript)

이태혁·2020년 9월 22일
0

encodeURI는 http://와 같은 URI에 관련된 부분은 encoding하지 않음
하지만 encodeURIComponent는 그런거 상관없이 다 인코딩함.

URI 전체를 인코딩하는 경우(http://포함) -> encodeURI
URI 부분만을 인코딩하는 경우 -> encodeURIComponent

let entireURL = "http://www.naver.com/네이버"

console.log(encodeURI(entireURL));
//결과: http://www.naver.com/%EB%84%A4%EC%9D%B4%EB%B2%84
console.log(encodeURIComponent(entireURL));
//결과: http%3A%2F%2Fwww.naver.com%2F%EB%84%A4%EC%9D%B4%EB%B2%84

The difference between encodeURI and encodeURIComponent is encodeURIComponent encodes the entire string, where encodeURI ignores protocol prefix ('http://') and domain name. encodeURIComponent is designed to encode everything, where encodeURI ignores a URL's domain related roots.

출처: https://love2dev.com/blog/whats-the-difference-between-encodeuri-and-encodeuricomponent/#:~:text=The%20difference%20between%20encodeURI%20and,a%20URL's%20domain%20related%20roots.

profile
back-end, cloud, docker, web의 관심이 있는 예비개발자입니다.

0개의 댓글