在线 URL 编码/解码 | encodeURIComponent / encodeURI | 实时演示
encodeURIComponent() 转义除了字母、数字、( ) . ! ~ * ' - _ 之外的所有字符。适合编码 URL 参数值。
encodeURI() 不会转义 URI 中具有特殊含义的字符:;/?:@&=+$,#。适合编码整个 URL。
解码函数 对应将编码后的字符串还原。
语法: encodeURIComponent(URIstring)
参数: URIstring,必需。一个字符串,含有 URI 组件或其他要编码的文本。
示例:
document.write(encodeURIComponent("https://www.sojson.com/encodeurl.html?我是个中文参数"))
document.write("<br />")
document.write(encodeURIComponent(",/?:@&=+$#"))
示例:
document.write(encodeURI("https://www.sojson.com/encodeurl.html?我是个中文参数"))
document.write("<br />")
document.write(encodeURI(",/?:@&=+$#"))
encodeURIComponent() 假定参数是 URI 的一部分(如查询参数值),会转义 ;/?:@&=+$,# 等分隔符。
encodeURI() 假定参数是完整 URI,不会转义这些保留字符,因此适合编码整个 URL。