JS中三種URI編碼方式對比分析
一、實例比較
數(shù)據(jù)傳遞常需要編碼后傳遞,接收還需反編譯,定義url:
var url = "https://www.cnblogs.com/?username='小森森'&password='666666'";
escape 與 unescape
console.log(escape(url));// 編碼 console.log(unescape(escape(url)));// 解碼
結(jié)果:
https%3A//www.cnblogs.com/%3Fusername%3D%27%u5C0F%u68EE%u68EE%27%26password%3D%27666666%27
encodeURIComponent 與 decodeURIComponent (推薦)
console.log(encodeURIComponent(url));// 編碼 console.log(decodeURIComponent(encodeURIComponent(url)));// 解碼
結(jié)果:
https%3A%2F%2Fwww.cnblogs.com%2F%3Fusername%3D'%E5%B0%8F%E6%A3%AE%E6%A3%AE'%26password%3D'666666'
encodeURI 與 decodeURI
console.log(encodeURI(url));// 編碼 console.log(decodeURI(encodeURI(url)));// 解碼
結(jié)果:
https://www.cnblogs.com/?username='%E5%B0%8F%E6%A3%AE%E6%A3%AE'&password='666666'
二、區(qū)別分析
三種方法都不會對 ASCII 字母、數(shù)字和規(guī)定的特殊 ASCII 標(biāo)點符號進行編碼,其余都替換為十六進制轉(zhuǎn)義序列.
escape 與 unescape
escape不編碼字符有69個:*,+,-,.,/,@,_,0-9,a-z,A-Z
對字符串全部進行轉(zhuǎn)義編碼,ECMAScript v3 反對使用該方法,對URL編碼勿使用此方法
encodeURIComponent 與 decodeURIComponent
encodeURIComponent不編碼字符有71個:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
傳遞參數(shù)時需使用encodeURIComponent,組合的url才不會被#等特殊字符截斷
encodeURI 與 decodeURI
encodeURI不編碼字符有82個:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
進行url跳轉(zhuǎn)時可以整體使用encodeURI,如果URI中含分隔符如 ? 和 #,應(yīng)使用encodeURIComponent
三、結(jié)論
推薦使用encodeURIComponent
到此這篇關(guān)于JS中三種URI編碼方式比較的文章就介紹到這了,更多相關(guān)js URI編碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JavaScript使用escape()、encodeURI()和decodeURI()實現(xiàn)URI編碼解碼
- js中編碼函數(shù):escape,encodeURI與encodeURIComponent詳解
- js 轉(zhuǎn)義字符及URI編碼詳解
- js中字符串編碼函數(shù)escape()、encodeURI()、encodeURIComponent()區(qū)別詳解
- 從此不再懼怕URI編碼 JavaScript及C# URI編碼詳解
- js編碼之encodeURIComponent使用介紹(asp,php)
- Javascript下的urlencode編碼解碼方法附decodeURIComponent
相關(guān)文章
javascript利用控件對windows的操作實現(xiàn)原理與應(yīng)用
假如要發(fā)送漢字的聊天框的內(nèi)容的話,我們也要從windows消息機制下手,先找到聊天消息的句柄(可以利用findwindow函數(shù)或者用spy工具哈),然后在找到上面的聊天框的句柄,接著我們就可以想這個句柄發(fā)送WM_SETTEXT的消息了2012-12-12
關(guān)于onScroll事件在IE6下每次滾動觸發(fā)三次bug說明
今天測試發(fā)現(xiàn)IE6下用window.onscroll,每次滾動時會觸發(fā)3次,而火狐、IE7沒此問題,應(yīng)該是IE6的一個BUG2011-09-09

