Cara menggunakan encode plain text javascript

Encoding and Decoding URI and URI components is a usual task in web development while making a GET request to API with query params. Many times construct a URL string with query params and in order to understand it, the response server needs to decode this URL. Browsers automatically encode the URL i.e. it converts some special characters to other reserved characters and then make the request. For eg: The space character ” ” is either converted to + or %20.

Show

    Example:

    • Open www.google.com and write a search query “geeks for geeks”.
    • After search results appear, observe the browser’s URL bar. The browser URL will consist %20 or + sign in place of space.
    • The URL will be displayed be like: https://www.google.com/search?q=geeks%20for%20geeks or https://www.google.com/search?q=geeks+for+geeks

    Note: The browser converted the spaces into + or %20 signs automatically.

    There are many other special characters and converting each of them by hardcode will be tedious. JavaScript provides the following functions to perform this task:

    Encoding a URL: Encoding in Javascript can be achieved using

    • encodeURI function
    • escape()

    JavaScript encodeURI Function: The encodeURI() function is used to encode complete URI. This function encodes the special character except for (, / ? : @ & = + $ #) characters.

    Syntax:

    encodeURI( complete_uri_string )

    Javascript




    https://www.google.com/search?q=geeks%20for%20geeks
    2

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    https://www.google.com/search?q=geeks%20for%20geeks
    4
    https://www.google.com/search?q=geeks%20for%20geeks
    5
    https://www.google.com/search?q=geeks%20for%20geeks
    6

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    https://www.google.com/search?q=geeks%20for%20geeks
    8

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    encodeURIComponent( uri_string_component )
    0

    encodeURIComponent( uri_string_component )
    1

    Output:

    https://www.google.com/search?q=geeks%20for%20geeks

    JavaScript encodeURIComponent() Function: The encodeURIComponent() function is used to encode some parts or components of URI. This function encodes the special characters. In addition, it encodes the following characters: , / ? : @ & = + $ #

    Syntax:

    encodeURIComponent( uri_string_component )

    Output:

    geeks%20for%20geeks

    Difference encodeURIComponenet and encodeURI:

     encodeURIComponentencodeURIDefinitionThe encodeURIComponent() function is used to encode some parts or components of URIThe encodeURI() function is used to encode complete URI. SyntaxencodeURIComponent( uri_string_component )encodeURI( complete_uri_string )Special Character Encoding This function encodes the special characters. In addition, it encodes the following characters: , / ? : @ & = + $ #This function encode the special character except (, / ? : @ & = + $ #) characters.

    JavaScript escape() function: This function takes a string as a single parameter & encodes the string that can be transmitted over the computer network which supports ASCII characters. Encoding is the process of converting plain text into ciphertext.

    Syntax:

    escape( string )

    Note: The escape() function only encodes the special characters, this function is deprecated.

    Exceptions: @ – + . / * _

    Javascript




    https://www.google.com/search?q=geeks%20for%20geeks
    2

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    https://www.google.com/search?q=geeks%20for%20geeks
    4
    https://www.google.com/search?q=geeks%20for%20geeks
    5
    https://www.google.com/search?q=geeks%20for%20geeks
    6

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    https://www.google.com/search?q=geeks%20for%20geeks
    8
    encodeURIComponent( uri_string_component )
    9

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    encodeURIComponent( uri_string_component )
    0

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    geeks%20for%20geeks
    3
    geeks%20for%20geeks
    4
    geeks%20for%20geeks
    5
    geeks%20for%20geeks
    6

    encodeURIComponent( uri_string_component )
    1

    Output:

    https://www.google.com/search?q=geeks%20for%20geeks
    https%3A//www.google.com/search%3Fq%3Dgeeks%20for%20geeks

    Decoding a URL: Decoding in Javascript can be achieved using

    • decodeURI() function.
    • unescape() function.

    JavaScript decodeURI() Function: The decodeURI() function is used to decode URI generated by encodeURI().

    Syntax:

    decodeURI( complete_encoded_uri_string )

    Example: This example describes the decodeURI() function of Javascript.

    Javascript




    https://www.google.com/search?q=geeks%20for%20geeks
    2

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    https://www.google.com/search?q=geeks%20for%20geeks
    4
    escape( string )
    1
    https://www.google.com/search?q=geeks%20for%20geeks
    6

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    escape( string )
    4

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    escape( string )
    6

    encodeURIComponent( uri_string_component )
    1

    Output:

    https://www.google.com/search?q=geeks for geeks

    JavaScript decodeURIComponent() Function: The decodeURIComponent() function is used to decode some parts or components of URI generated by encodeURIComponent().

    Syntax:

    decodeURIComponent( encoded_uri_string_component )

    Example: This example describes the decodeURIComponent() of Javascript.

    Javascript




    https://www.google.com/search?q=geeks%20for%20geeks
    2

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    https://www.google.com/search?q=geeks%20for%20geeks
    https%3A//www.google.com/search%3Fq%3Dgeeks%20for%20geeks
    0
    https://www.google.com/search?q=geeks%20for%20geeks
    https%3A//www.google.com/search%3Fq%3Dgeeks%20for%20geeks
    1

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    https://www.google.com/search?q=geeks%20for%20geeks
    https%3A//www.google.com/search%3Fq%3Dgeeks%20for%20geeks
    3

    https://www.google.com/search?q=geeks%20for%20geeks
    3
    https://www.google.com/search?q=geeks%20for%20geeks
    https%3A//www.google.com/search%3Fq%3Dgeeks%20for%20geeks
    5

    encodeURIComponent( uri_string_component )
    1

    Output:

    geeks for geeks

    Difference decodeURIComponent and decodeURI:

     decodeURIComponentdecodeURIDefinitionThe decodeURIComponent() function is used to decode some parts or components of URI generated by encodeURIComponent().Decoding in Javascript can be achieved using decodeURI function.SyntaxdecodeURIComponent( encoded_uri_string_component )decodeURI( complete_encoded_uri_string )Special Character Encoding It takes encodeURIComponent(url) string so it can decode these characters.It takes encodeURI(url) string so it cannot decoded characters (, / ? : @ & = + $ #)Example

    decodeURIComponent(“%41”) It returns “A”

    decodeURIComponent(“%26”): It returns “&”

    decodeURI(“%41”): It returns “A”

    decodeURI(“%26”): It returns “%26”

    JavaScript unescape() Function: This function takes a string as a single parameter and uses it to decode that string encoded by the escape() function. The hexadecimal sequence in the string is replaced by the characters they represent when decoded via unescape() function.