URL编码解码工具

2024-10-22

encodeURI编码,不会对特殊符号编码

encodeURIComponent编码方式,会对特殊符号编码


工具介绍

URL编码解码工具介绍

url编码解码,又叫百分号编码,是统一资源定位(URL)编码方式。URL地址(常说网址)规定了常用地数字,字母可以直接使用,另外一批作为特殊用户字符也可以直接用(/,:@等),剩下的其它所有字符必须通过%xx编码处理。

为了让包含中文的URL网址可以使用,您可以使用本工具对中文进行UrlEncode编码。

JS原生的escape只是将中文转换为unicode编码,encodeURI或者encodeURIComponent也是对于中文unicode编码的url再编码,js本身没有特性支持gbk的urlencode,大部分的时候我们是通过后台(java,urlencode.encode(“中文”,”gbk”))这样来实现的,本工具是使用JS实现,这样就可以实现浏览器的完美兼容。

urlencode是一个函数,可将字符串以URL编码,用于编码处理。 URL编码(URL encoding),也称作百分号编码(Percent-encoding), 是特定上下文的统一资源定位符 (URL)的编码机制。 适用于统一资源标识符(URI)的编码,也用于为"application/x-www-form-urlencoded" MIME准备数据, 因为它用于通过HTTP的请求操作(request)提交HTML表单数据。

一、encodeURIComponent()

1.encodeURIComponent()方法的使用

语法:encodeURIComponent(URIstring)

参数:URIstring,必需。一个字符串,含有 URI 组件或其他要编码的文本。

2.encodeURIComponent()测试

测试

document.write(encodeURIComponent("https://www.zhanid.com/tool/urlencode.html/?站长工具网"))
document.write("<br />")
document.write(encodeURIComponent(",/?:@&=+$#"))

结果

https%3A%2F%2Fwww.zhanid.com%2Ft%2Furlencode%2F%3F%E7%AB%99%E9%95%BF%E5%B7%A5%E5%85%B7%E7%BD%91

二、encodeURI()测试

测试

document.write(encodeURI("https://www.zhanid.com/tool/urlencode.html/?站长工具网"))
document.write("<br />")
document.write(encodeURI(",/?:@&=+$#"))

结果

https://www.zhanid.com/tool/urlencode.html/?%E7%AB%99%E9%95%BF%E5%B7%A5%E5%85%B7%E7%BD%91

三、encodeURIComponent() 函数 与 encodeURI() 函数的区别

请注意 encodeURIComponent() 函数 与 encodeURI() 函数的区别之处,前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。