encodeURI和encodeURIComponent
window.encodeURI函数用来编码一个URL,但是不会对以下字符进行编码:“:”, “/”, “;”, “?”.
window.encodeURIComponent则会对上述字符进行编码。
我们通过一个例子来说明:
|
1
2
|
'index.jsp?page='
+encodeURI(
'/page/home.jsp'
);
// "index.jsp?page=/page/home.jsp"
'index.jsp?page='
+encodeURIComponent(
'/page/home.jsp'
);
// "index.jsp?page=%2Fpage%2Fhome.jsp"
|
因此,在对URL进行编码时我们经常会选择 encodeURIComponent。
本文解释了window.encodeURI和window.encodeURIComponent的区别,说明在URL编码时如何选择使用其中之一。
638

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



