最近項(xiàng)目開發(fā)中,關(guān)于頁面跳轉(zhuǎn)或ajax傳輸數(shù)據(jù)時,常用到encodeURIComponent全局函數(shù),
目的在于將字符串編碼為uri組件進(jìn)行傳遞,否則字符串在傳遞過程中會被解析或者一些特殊字符,/?@:;&=+$#*無法成功接收而導(dǎo)致代碼出錯。
此函數(shù)假定參數(shù)URIstring為uri的一部分(比如協(xié)議、主機(jī)名、路徑或查詢字符串)
常用:
<script>
document.write(encodeURIComponent("http://www.w3school.com.cn")+ "<br />")
document.write(encodeURIComponent("http://www.w3school.com.cn/My first/")+ "<br />")
document.write(encodeURIComponent(",/?:@&=+$#"))
</script>
另外還有編碼相關(guān)的全局函數(shù)也可使用
encodeURI();
decodeURI();
encodeURIComponent();
decodeURIComponent();