2012-11-26 43 views

回答

6

該函數對特殊字符進行編碼。此外,它編碼以下字符:,/? : @ & = + $ # .

這個定義對於什麼「特殊字符」是模糊的。這聽起來像是encodeURIencodeURIComponent之間的比較。兩者都將正確地跳轉\,因爲%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"