JavaScript programming language provides different encoding functions with some little differences. These functions can be used to encode the URL. Encoding URL is useful to prevent errors, especially during transmission. Encoding URL will make the URL consist of only UTF-8 characters. For example space, Tilda, etc can be expressed in a proper way by encoding.
JavaScript编程语言提供了不同的编码功能,但差别不大。 这些功能可用于编码URL。 编码URL有助于防止错误,尤其是在传输过程中。 编码URL将使URL仅包含UTF-8字符。 例如,空间,蒂尔达(Tilda)等可以通过编码以适当的方式表示。
JavaScript编码功能 (JavaScript Encoding Functions)
JavaScript provides 3 encoding functions. These functions can be used according to the situation.
JavaScript提供3种编码功能。 可以根据情况使用这些功能。
- `escape` 逃生
- `encodeURI()``encodeURI()`
- `encodeURIComponent()` `encodeURIComponent()`
使用escape()函数编码 (Encode with escape() Function)
escape()
is the simplest function that is deprecated in JavaScript version 1.5. It is mainly used to encode strings. * @ - _ + . /
are exception which are not encoded by the escape() function. As escape() function is depracated use encodeURI()
and encodeURIComponent()
functions instead.
escape()
是JavaScript 1.5版中不推荐使用的最简单的函数。 它主要用于编码字符串。 * @ - _ + . /
* @ - _ + . /
是没有由escape()函数编码的异常。 随着escape()函数的使用被弃用,请改为使用encodeURI()
和encodeURIComponent()
函数。
var char_set1 = ";,/?:@&=+$"; // Reserved Characters
var char_set2 = "-_.!~*'()"; // Unescaped Characters
var char_set3 = "#"; // Number Sign
var char_set4 = "ABC abc 123"; // Alphanumeric Characters + Space
var char_set5 = "poftut.com/about"; //Some normal URL to encode
var char_set6 = "İsmail Baydan" //Some name
escape(char_set1);
escape(char_set2);
escape(char_set3);
escape(char_set4);
escape(char_set5);
escape(char_set6);
使用encodeURI()函数进行编码(Encode with encodeURI() Function)
encodeURI()
function is used to encode Uniform Resource Identifier or URI which is alternatively used for URL.
encodeURI()
函数用于对统一资源标识符或URI进行编码,或者将其用于URL。
var char_set1 = ";,/?:@&=+$"; // Reserved Characters
var char_set2 = "-_.!~*'()"; // Unescaped Characters
var char_set3 = "#"; // Number Sign
var char_set4 = "ABC abc 123"; // Alphanumeric Characters + Space
var char_set5 = "poftut.com/about"; //Some normal URL to encode
var char_set6 = "İsmail Baydan" //Some name
encodeURI(char_set1);
encodeURI(char_set2);
encodeURI(char_set3);
encodeURI(char_set4);
encodeURI(char_set5);
encodeURI(char_set6);
使用encodeURIComponent()函数进行编码(Encode with encodeURIComponent() Function)
encodeURIComponent()
is another function used to encode given URL. encodeURIComponent() function is more useful in order to encode URLs. encodeURIComponent() will encode & properly which can cause jeopardize the integrity of the data.
encodeURIComponent()
是另一个用于编码给定URL的函数。 encodeURIComponent()函数对于编码URL更为有用。 encodeURIComponent()会正确编码并正确执行,这可能会危害数据的完整性。
var char_set1 = ";,/?:@&=+$"; // Reserved Characters
var char_set2 = "-_.!~*'()"; // Unescaped Characters
var char_set3 = "#"; // Number Sign
var char_set4 = "ABC abc 123"; // Alphanumeric Characters + Space
var char_set5 = "poftut.com/about"; //Some normal URL to encode
var char_set6 = "İsmail Baydan" //Some name
encodeURIComponent(char_set1);
encodeURIComponent(char_set2);
encodeURIComponent(char_set3);
encodeURIComponent(char_set4);
encodeURIComponent(char_set5);
encodeURIComponent(char_set6);
翻译自: https://www.poftut.com/how-to-encode-url-in-javascript-tutorial-with-examples/