encodeURI() encodeURIComponent()

URI: Uniform Resource Identifiers,通用资源标识符

Global对象的encodeURI()和encodeURIComponent()方法可以对URI进行编码,以便发送给浏览器。

有效的URI中不能包含某些字符,例如空格。而这URI编码方法就可以对URI进行编码,它们用特殊的UTF-8编码替换所有无效的字 符,从而让浏览器能够接受和理解。

编码

encodeURI()   对整个URI编码          (例如,http://www.jxbh.cn/illegal value.htm)

encodeURIComponent()    对URI中的某一段编码             (例如前面URI中的illegal value.htm)

解码

decodeURIComponent()


区别:

encodeURI()不会对本身属于URI的特殊字符进行编码,例如冒号、正斜杠、问号 、#;

encodeURIComponent()则会对任何非标准字符进行编码。来看下面的例子:


var uri="http://www.jxbh.cn/illegal value.htm#start";
encodeURI (uri)       //     ”http: //www.jxbh.cn/illegal%20value .htm#start”
encodaURIComponent (uri);   //       ”http% 3A%2F%2Fwww.jxbh.cn%2 Fillegal%2 0value. htm%23 start”

一般来说,我们使用encodeURIComponent()方法的时候要比使用encodeURI()更多,因为在实践中更常见的是对查询字符串参数而不是对基础URL进行编码.

猜你喜欢

转载自blog.csdn.net/wang_gongzi/article/details/82853424