var encoded_uri_comp = encodeURIComponent(uri_component);
The encodeURIComponent function returns an encoded URI component by replacing all special characters with their corresponding UTF-8 escape sequences.
encodeURIComponent does not encode the following characters:
~ ! * ( ) '
Here's an example of encoding a URI component that contains letters with diacritics:
The above code will output:
Below is an example of encoding a URI that contains Cyrillic letters:
The output of the above code will be: