encodeURI and decodeURI

encodeURI and decodeURI是js內置的編碼及解碼方法,用於給瀏覽器路徑中的非英文字符編碼。

URI Encode 與 URI Encode Component 的區別

(參見https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI

URI Encode (encodeURI):對整個 URI 進行編碼,主要用於完整的網址。它不會編碼以下保留字元:

A–Z a–z 0–9 - _ . ! ~ * ' ( )

; / ? : @ & = + $ , #

這些字元在 URI 中有特殊含義,因此適合用於編碼完整的 URL。

URI Encode Component (encodeURIComponent):對 URI 的單個組件進行編碼,會將所有特殊字元(包括保留字元)轉換為編碼格式。適用於編碼 URL 中的查詢參數或片段,例如鍵值對中的值。

URI Decode (decodeURI):對使用 encodeURI 編碼的內容進行解碼,恢復原始文字,但無法解碼 encodeURIComponent 編碼的內容。

URI Decode Component (decodeURIComponent):對使用 encodeURIComponent 編碼的內容進行解碼,適用於單個組件的解碼,能處理所有特殊字元的解碼。

Leave a Comment