encodeURI 可將字串編碼為 URI,常用於處理包含中文或空格的網址。
encodeURI("https://example.com/My first/");
// https://example.com/My%20first/
encodeURI 不會編碼 ASCII 字母、數字,以及 - _ . ! ~ * ' ( ) 等符號。它也不會編碼 URI 中具有特殊意義的 ; / ? : @ 與 = + $ #。
若需要編碼 URI 元件,例如查詢參數值,建議使用 encodeURIComponent,避免特殊符號被保留而影響解析。