如何使用示例对JavaScript教程中的URL进行编码?

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);
Encode with escape() Function
Encode with escape() Function
使用escape()函数编码

使用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);
Encode with encodeURI() Function
Encode with encodeURI() Function
使用encodeURI()函数进行编码

使用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);
Encode with encodeURIComponent() Function
Encode with encodeURIComponent() Function
使用encodeURIComponent()函数进行编码
LEARN MORE  What Is Website and Its Definition?
了解更多什么是网站及其定义?

翻译自: https://www.poftut.com/how-to-encode-url-in-javascript-tutorial-with-examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值