Home  >  Article  >  Web Front-end  >  How to encode and convert url in javascript

How to encode and convert url in javascript

青灯夜游
青灯夜游Original
2021-04-09 18:56:1312152browse

Javascript method of encoding and converting URLs: 1. Use the encodeURI() function to encode the string as a URI; 2. Use the encodeURIComponent() function to encode the string as a URI component.

How to encode and convert url in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In the project, the url encountered a problem of failure to parse parameters and array objects. It was solved by transcoding, but the actual principle was not clear. I studied JS transcoding and decoding.

There are three functions in Javascript language used for encoding.

  • escape()

  • encodeURI()
  • encodeURIComponent()

The difference between three encoding methods:

  • escape and unescape:

    escape: cannot Used directly for URL encoding, its real function is to return the Unicode encoding value of a character, so the specified string can be encoded.
    Except for ASCII letters, numbers, and punctuation marks "@ * _ - . /", all spaces, punctuation marks, special characters, and other non-ASCII characters will be converted into characters in the format of %xx (hexadecimal numbers) coding. That is, escape does not encode characters. There are 69 characters : *, , -, ., /, @, _, 0-9, a-z, A-Z.

    The corresponding decoding function is unescape();

  • ##encodeURI and decodeURI: **encodeURI()** is the function actually used to encode URLs in Javascript. The entire URL is encoded. In addition to common symbols, symbols with special meanings in the URL "; / ? : @ & = $ , #" are not encoded. After encoding, output the UTF-8 form of the symbol and add % before each byte. Note that encodeURI() does not encode single quotes. The corresponding decoding function of

    is

    decodeURI().

  • encodeURIComponent and decodeURIComponent

    encodeURIComponent are used to individually encode the components of the URL. , rather than encoding the entire URL. Therefore, the symbols "; / ? : @ & = $ , #" that are not encoded in encodeURI() will all be encoded in encodeURIComponent().

    Because encodeURIComponent() will encode more characters, such as "/" and other characters. If the string contains several parts of the URI, this cannot be used for encoding. Otherwise, the URL will display incorrectly after the "/" character is encoded. Therefore, when passing parameters, parameters containing special characters may cause interruptions.

To sum up:

**escape()** function is used by js to encode strings. Not commonly used

**encodeURI()** is used to jump the entire url.

**encodeURIComponent()** is used to pass parameters. Parameters containing special characters may cause interruptions.

[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of How to encode and convert url in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn