javascript
JavaScript编码encode和decode escape和unescape
encodeURI() 函數(shù)可把字符串作為 URI 進行編碼。
語法
encodeURI(URIstring)| URIstring | 必需。一個字符串,含有 URI 或其他要編碼的文本。 |
返回值
URIstring 的副本,其中的某些字符將被十六進制的轉義序列進行替換。
說明
該方法不會對 ASCII 字母和數(shù)字進行編碼,也不會對這些 ASCII 標點符號進行編碼: - _ . ! ~ * ' ( ) 。
該方法的目的是對 URI 進行完整的編碼,因此對以下在 URI 中具有特殊含義的 ASCII 標點符號,encodeURI() 函數(shù)是不會進行轉義的:;/?:@&=+$,#
提示和注釋
提示:如果 URI 組件中含有分隔符,比如 ? 和 #,則應當使用 encodeURIComponent() 方法分別對各組件進行編碼
<!doctype html> <html> <head> <meta charset="utf-8"/> <title></title> </head> <body> <script type="text/javascript"> //測試編碼URI的函數(shù)var url="http://www.nframes.com.cn/index.html?search=m %^^$#e";var res=encodeURI(url);document.write(res);var res1=decodeURI(res);document.write('<br/>'+res1+'<br/>'); </script> </body> </html>結果:
http://www.nframes.com.cn/index.html?search=m%20%25%5E%5E$#e http://www.nframes.com.cn/index.html?search=m %^^$#e?
JavaScript escape() 函數(shù)
JavaScript 全局對象
定義和用法
escape() 函數(shù)可對字符串進行編碼,這樣就可以在所有的計算機上讀取該字符串。
語法
escape(string)| string | 必需。要被轉義或編碼的字符串。 |
返回值
已編碼的 string 的副本。其中某些字符被替換成了十六進制的轉義序列。
說明
該方法不會對 ASCII 字母和數(shù)字進行編碼,也不會對下面這些 ASCII 標點符號進行編碼: * @ - _ + . / 。其他所有的字符都會被轉義序列替換。
提示和注釋
提示:可以使用 unescape() 對 escape() 編碼的字符串進行解碼。
注釋:ECMAScript v3 反對使用該方法,應用使用 decodeURI() 和 decodeURIComponent() 替代它。
實例
在本例中,我們將使用 escape() 來編碼字符串:
<!doctype html> <html> <head> <meta charset="utf-8"/> <title></title> </head> <body> <script type="text/javascript"> //測試編碼URI的函數(shù)var url="http://www.nframes.com.cn/index.html?search=m %^^$#e";var res=escape(url);document.write(res);var res1=unescape(res);document.write('<br/>'+res1+'<br/>'); </script> </body> </html>?
輸出:
http%3A//www.nframes.com.cn/index.html%3Fsearch%3Dm%20%25%5E%5E%24%23e http://www.nframes.com.cn/index.html?search=m %^^$#e?
轉載于:https://www.cnblogs.com/Yimi/p/6344597.html
總結
以上是生活随笔為你收集整理的JavaScript编码encode和decode escape和unescape的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt 自定义界面(实现无边框、可移动)
- 下一篇: QT 提示之右下角冒泡