<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<script type="text/javascript">
var url = 'http://localhost:53756/url编码.html';
var result = window.encodeURI(url);//encodeURI()方法只是将需要编码的部分进行编码
alert(result);//输出http://localhost:53756/url%E7%BC%96%E7%A0%81.html
var result2 = window.encodeURIComponent(url);//encodeURIComponent()方法是将整个url都进行编码,这样编码后在服务器是识别不出来的,所以使用时只对需要编码的部分进行编码
alert(result2);//输出http%3A%2F%2Flocalhost%3A53756%2Furl%E7%BC%96%E7%A0%81.html
</script>
</body>
</html>
url编码
本文深入探讨了URL编码的两种主要方法:encodeURI()和encodeURIComponent()。通过实例对比,展示了这两种方法在处理URL中特殊字符时的不同之处,encodeURI()仅编码需要的部分,而encodeURIComponent()则对整个URL进行编码。

被折叠的 条评论
为什么被折叠?



