欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS中三種URI編碼方式對比分析

 更新時間:2024年01月03日 09:34:11   作者:LiangSenCheng小森森  
這篇文章主要介紹了JS中三種URI編碼方式對比分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

一、實(shí)例比較

數(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)點(diǎn)符號進(jìn)行編碼,其余都替換為十六進(jìn)制轉(zhuǎn)義序列.

escape 與 unescape

escape不編碼字符有69個:*,+,-,.,/,@,_,0-9,a-z,A-Z

對字符串全部進(jìn)行轉(zhuǎn)義編碼,ECMAScript v3 反對使用該方法,對URL編碼勿使用此方法

encodeURIComponent 與 decodeURIComponent

encodeURIComponent不編碼字符有71個:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

傳遞參數(shù)時需使用encodeURIComponent,組合的url才不會被#等特殊字符截?cái)?/p>

encodeURI 與 decodeURI

encodeURI不編碼字符有82個:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

進(jìn)行url跳轉(zhuǎn)時可以整體使用encodeURI,如果URI中含分隔符如 ? 和 #,應(yīng)使用encodeURIComponent

三、結(jié)論

推薦使用encodeURIComponent

到此這篇關(guān)于JS中三種URI編碼方式比較的文章就介紹到這了,更多相關(guān)js URI編碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論