encodeURIComponent() 函數(shù)可把字符串作為 URI 組件進(jìn)行編碼。
encodeURIComponent(URIstring)
參數(shù) | 描述 |
---|---|
URIstring | 必需。一個(gè)字符串,含有 URI 組件或其他要編碼的文本。 |
URIstring 的副本,其中的某些字符將被十六進(jìn)制的轉(zhuǎn)義序列進(jìn)行替換。
該方法不會(huì)對(duì) ASCII 字母和數(shù)字進(jìn)行編碼,也不會(huì)對(duì)這些 ASCII 標(biāo)點(diǎn)符號(hào)進(jìn)行編碼: - _ . ! ~ * ' ( ) 。
其他字符(比如 :;/?:@&=+$,# 這些用于分隔 URI 組件的標(biāo)點(diǎn)符號(hào)),都是由一個(gè)或多個(gè)十六進(jìn)制的轉(zhuǎn)義序列替換的。
提示:請(qǐng)注意 encodeURIComponent() 函數(shù) 與 encodeURI() 函數(shù)的區(qū)別之處,前者假定它的參數(shù)是 URI 的一部分(比如協(xié)議、主機(jī)名、路徑或查詢字符串)。因此 encodeURIComponent() 函數(shù)將轉(zhuǎn)義用于分隔 URI 各個(gè)部分的標(biāo)點(diǎn)符號(hào)。
在本例中,我們將使用 encodeURIComponent() 對(duì) URI 進(jìn)行編碼:
<script type="text/javascript"> document.write(encodeURIComponent("http://theartemis.cn")) document.write("<br />") document.write(encodeURIComponent("http://theartemis.cn/p 1/")) document.write("<br />") document.write(encodeURIComponent(",/?:@&=+$#")) </script>
輸出:
http%3A%2F%2Ftheartemis.cn http%3A%2F%2Ftheartemis.cn%2Fp%201%2F %2C%2F%3F%3A%40%26%3D%2B%24%23
如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答?。?點(diǎn)擊進(jìn)入論壇
Powered by 365建站網(wǎng) RSS地圖 HTML地圖
copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)