Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 1.13 KB

basic: encodeURI vs encodeURIComponent.md

File metadata and controls

34 lines (25 loc) · 1.13 KB

js: encodeURI vs encodeURIComponent

encodeURI won't encode following chars while encodeURIComponent will.

;,/?:@&=+$#

A simple example:

> a = "http://u:[email protected]/123abc?name=张三&age=18#login"
'http://u:[email protected]/123abc?name=张三&age=18#login'
> encodeURI(a)
'http://u:[email protected]/123abc?name=%E5%BC%A0%E4%B8%89&age=18#login'
> encodeURIComponent(a)
'http%3A%2F%2Fu%3Apass%40www.abc.com%2F123abc%3Fname%3D%E5%BC%A0%E4%B8%89%26age%3D18%23login'

The :/&#@ that commonly used in url are also encoded when using encodeURIComponent.

If you need to use it in url query string like abc.com/login?redirect=REDIRECT, the REDIRECT here must be encoded using encodeURIComponent. Otherwise, if it has substr like &a=b, it will be parsed as query string of the new url but not the redirect params.

    location.href = `abc.com/login?redirect=${location.href}`        // bad
    location.href = `abc.com/login?redirect=${encodeURIComponent(location.href)}`     // good

references