how do I properly encode a URL in JavaScript?
I am working on a browser plugin that takes the URL of the current page or any selected link as a parameter and send it to our server.
When characters of the basic latin alphabet are present in the url (like http://en.wikipedia.org/wiki/Vehicle), the plugin works fine. However, when the URL contains characters from another alphabet such as http://ru.wikipedia.org/wiki/Коляска the plugin does not work, I do use the encodeURIComponent
method but that does not seem to solve t开发者_开发百科he issue. Any idea?
Thanks,
Olivier.
You probably want to use encodeURI
/decodeURI
, if you are trying to take a full URI with non-ASCII characters and translate it into its encoded form. These preserve special URI characters, such as :
and /
, instead of escaping them; it only escapes non-ASCII characters and characters that are invalid in URIs. Thus, they do essentially the same thing as typing in the address bar or putting a URI in an <a href="...">
(though the behavior might vary somewhat between browser, and isn't exactly the same).
encodeURIComponent
is intended for encoding only a single component of a URI, replacing special characters that are meaningful in URIs, so that you can use the component as a query parameter or path component in a longer URI.
This one says it covers UTF-8. http://www.webtoolkit.info/javascript-url-decode-encode.html. Might solve your problem
精彩评论