encodeURI()、encodeURIComponent()区别及使用场景

一、区别:

  • encodeURI()
    encodeURI()通常用于转码整个 URL,不会对URL 元字符以及语义字符进行转码,URL元字符:

URL 元字符:分号(;),逗号(,),斜杠(/),问号(?),冒号(:),at(@),&,等号(=),加号(+),美元符号($),井号(#)

语义字符:a-z,A-Z,0-9,连词号(-),下划线(_),点(.),感叹号(!),波浪线(~),星号(*),单引号('),圆括号(())

  • encodeURIComponent()

encodeURIComponent()通常只用于转码URL组成部分,如URL中?后的一串;会转码除了语义字符之外的所有字符,即元字符也会被转码。

注:若整个链接被encodeURIComponent()转码,则该链接无法被浏览器访问,需要解码之后才可以正常访问。

二、使用场景:

正常情况下我们使用的encodeURI,比如:

console.log(encodeURI("http://example.com/pagephp?id=123&age=23&sex=\\gril"));
// http://example.com/pagephp?id=123&age=23&sex=%5Cgril

什么时候使用encodeURIComponent:

参数中有回调地址时使用encodeURIComponent

https://www.baidu.com/s?returnURL=http://www.test.com/

链接内包含一个回调地址,回调地址是另外一个URL,此时我们就需要使用encodeURIComponent()对回调地址进行转码,这样一来,URL中就不会出现多个http://,多个&这样的特殊字符;方便对回调地址进行处理;以上链接处理如下:

// 对URL中的回调链接进行转码
'https://www.baidu.com/s?returnURL=' +encodeURIComponent('http://www.test.com/')
//输出: "https://www.baidu.com/s?returnURL=http%3A%2F%2Fwww.test.com%2F"

猜你喜欢

转载自blog.csdn.net/qq_42931285/article/details/134626060
今日推荐