DIV CSS 佈局教程網

encodeURI、encodeURIComponent 與 escape 的差別與正確用法
編輯:關於JavaScript     

JavaScript 裡有這麼三個編碼函式,看起來功能重疊,實際使用時卻常常選錯。本文把它們的差異與適用場合一次講清楚。

先認識編碼與解碼的配對

encodeURI() 與 encodeURIComponent() 主要用在 URL 的編解碼,各自對應的解碼函式是 decodeURI() 與 decodeURIComponent()。用法很簡單,把要轉換的字串當參數傳進去即可:

 encodeURI("我是要編碼的文字")
decodeURI("我是要解碼的文字")
encodeURIComponent("我是要編碼的文字")
decodeURIComponent("我是要解碼的文字")

encodeURI 與 encodeURIComponent 的差別

主要差異在於保留字元:

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

encodeURIComponent 不編碼的字元有 71 個:! ' ( ) * - . _ ~ 以及 0-9、a-z、A-Z

也就是說,encodeURIComponent 會把 ;、/、?、:、@、&、=、+、$、,、# 這些在 URL 中有特殊意義的字元一起編碼掉。

encodeURI 主要用於整個位址要直接賦值給網址列時:

 location.href=encodeURI("https://www.cnblogs.com/Tezml/");

encodeURIComponent 主要用於 URL 的查詢參數:

 location.href="https://www.cnblogs.com/Tezml/test.php?a="+encodeURIComponent("我就是我");

escape 的定位完全不同

escape() 是編碼,unescape() 是解碼。

escape 方法把字串編碼,使其能在所有電腦上可讀。

 escape(charString)

charString 參數是要編碼的任意字串物件或字面值。escape 方法會回傳一個包含 charString 內容的字串值(Unicode 格式)。所有空格、標點、重音符號以及其他非 ASCII 字元,都會以 %xx 編碼取代,其中 xx 是該字元的十六進位數值。例如空格會回傳 "%20"。字元值大於 255 的則以 %uxxxx 格式儲存。

escape 不編碼的字元有 69 個:* + - . / @ _ 以及 0-9、a-z、A-Z

請注意:escape 方法不能作為 URI 的編碼工具,這件事必須交由 encodeURI 與 encodeURIComponent 處理。

實測對照

 <!DOCTYPE html>
<html>
<head>
<title>Tezml_編碼解碼測試</title>
<meta charset="utf-8">
<meta name="author" content="Tezml" />
<meta name="copyright" content="Tezml" />
<meta name="description" content="Tezml" />
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<div id="wz1"></div>
<div id="wz2"></div>
<div id="wz3"></div>
<div id="wz4"></div>
<div id="wz5"></div>
<div id="wz6"></div>
<div id="wz7"></div>
<div id="wz8"></div>
<div id="wz9"></div>
<div id="wz10"></div>
<div id="wz11"></div>
<div id="wz12"></div>
</body>
<script type="text/javascript">
var chinese="請叫我中文"
var english="place tall me englash"
var Monster=":#&$/@"
$("#wz1").html(encodeURI(chinese))//編碼 %E8%AF%B7%E5%8F%AB%E6%88%91%E4%B8%AD%E6%96%87
$("#wz2").html(decodeURI(chinese))//解碼 請叫我中文
$("#wz3").html(encodeURI(english))//編碼 place%20tall%20me%20englash
$("#wz4").html(decodeURI(english))//解碼 place tall me englash
$("#wz5").html(encodeURIComponent(Monster))//編碼 %3A%23%26%24%2F%40
$("#wz6").html(encodeURI(Monster))//編碼 :#&$/@
$("#wz7").html(escape(chinese))//編碼 %u8BF7%u53EB%u6211%u4E2D%u6587
$("#wz8").html(escape(english))//編碼 place%20tall%20me%20englash
$("#wz9").html(escape(Monster))//編碼 %3A%23%26%24/@
$("#wz10").html(unescape(chinese))//編碼 請叫我中文
$("#wz11").html(unescape(english))//編碼 place tall me englash
$("#wz12").html(unescape(Monster))//編碼 :#&$/@
</script>
</html>

總結與選用建議

escape() 不能直接用於 URL 編碼,它真正的作用是回傳字元的 Unicode 編碼值。例如「春節」會得到 %u6625%u8282。escape 不會編碼 "+",主要用途是中文編碼,如今已不建議使用。

encodeURI() 是 JavaScript 中真正用來對 URL 編碼的函式。它編碼整個位址,但會保留 ; / ? : @ & = + $ , # 這些有特殊含義的字元,對應解碼函式為 decodeURI()。

encodeURIComponent() 則會把上述特殊字元一併編碼,對應解碼函式為 decodeURIComponent()。

實務上的判斷很簡單:編碼整段網址用 encodeURI,編碼要放進查詢參數的單一值用 encodeURIComponent。若要傳遞的網址本身含有 & 符號,就一定要用 encodeURIComponent,否則 & 會被伺服器當成參數分隔符,造成內容被截斷。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved