1
w3schools說以下有關encodeURIComponent
功能:JavaScript的encodeURIComponent方法用反斜槓
該功能將特殊字符編碼。此外, 它編碼以下字符:
,/? : @ & = + $ #
。
這是否意味着它不能編碼反斜槓(\
)?
w3schools說以下有關encodeURIComponent
功能:JavaScript的encodeURIComponent方法用反斜槓
該功能將特殊字符編碼。此外, 它編碼以下字符:
,/? : @ & = + $ #
。
這是否意味着它不能編碼反斜槓(\
)?
該函數對特殊字符進行編碼。此外,它編碼以下字符:
,/? : @ & = + $ # .
這個定義對於什麼「特殊字符」是模糊的。這聽起來像是encodeURI
和encodeURIComponent
之間的比較。兩者都將正確地跳轉\
,因爲%5C
,所以你不必擔心反斜槓。
encodeURI
將離開所列出的字符,因爲它假設是,整個URI被編碼:
encodeURI('http://example.com/foo bar/baz.html');
//produces "http://example.com/foo%20bar/baz.html"
encodeURIComponent
將逸出一切因爲假定該字符串是要用作的一部分查詢字符串:
'http://example.com?foo=' + encodeURIComponent('http://example.com/fizz/buzz.html');
//produces "http://example.com?foo=http%3A%2F%2Fexample.com%2Ffizz%2Fbuzz.html"