且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JavaScript中对utf-8字符串使用encodeURI()与escape()

更新时间:2023-02-25 14:56:31

嗨!



当涉及到 escape unescape ,我按两个规则生活:


  1. 避免他们你很容易就可以。

  2. 否则,使用它们。





如问题所述, escape unescape 已被弃用。一般来说,应该避免使用不推荐的功能。



所以,如果 encodeURIComponent encodeURI 为您做点窍门,您应该使用而不是 escape



使用他们当你不能轻易地避免他们:



浏览器将尽可能努力实现向后兼容性。所有主流浏览器已经实现了 escape unescape ;为什么不执行它们?



浏览器必须重新定义 escape unescape 如果新的规范要求他们这样做。可是等等!编写规格的人很聪明。他们也有兴趣不违反向后兼容性!



我意识到上述论据很弱。但是相信我,...当谈到浏览器时,弃用的东西有用。这甚至包括不推荐的HTML标签,如< xmp> < center>



使用 escape unescape



所以自然地,下一个问题是,何时使用 escape unescape



最近,在 CloudBrave 工作时,我不得不处理 utf8 latin1 和互换。



阅读一堆博文,我意识到这是多么简单:

  var utf8_to_latin1 = function(s){
return unescape(encodeURIComponent ));
};
var latin1_to_utf8 = function(s){
return decodeURIComponent(escape(s));
};

这些互换,不使用 escape unescape 相关。不要避免 escape unescape ,生活变得更简单。



希望这有帮助。


I am handling utf-8 strings in JavaScript and need to escape them.

Both escape() / unescape() and encodeURI() / decodeURI() work in my browser.

escape()

> var hello = "안녕하세요"
> var hello_escaped = escape(hello)
> hello_escaped
  "%uC548%uB155%uD558%uC138%uC694"
> var hello_unescaped = unescape(hello_escaped)
> hello_unescaped
  "안녕하세요"

encodeURI()

> var hello = "안녕하세요"    
> var hello_encoded = encodeURI(hello)
> hello_encoded
  "%EC%95%88%EB%85%95%ED%95%98%EC%84%B8%EC%9A%94"
> var hello_decoded = decodeURI(hello_encoded)
> hello_decoded
  "안녕하세요"

However, Mozilla says that escape() is deprecated.

Although encodeURI() and decodeURI() work with the above utf-8 string, the docs (as well as the function names themselves) tell me that these methods are for URIs; I do not see utf-8 strings mentioned anywhere.

Simply put, is it okay to use encodeURI() and decodeURI() for utf-8 strings?

Hi!

When it comes to escape and unescape, I live by two rules:

  1. Avoid them when you easily can.
  2. Otherwise, use them.

Avoiding them when you easily can:

As mentioned in the question, both escape and unescape have been deprecated. In general, one should avoid using deprecated functions.

So, if encodeURIComponent or encodeURI does the trick for you, you should use that instead of escape.

Using them when you can't easily avoid them:

Browsers will, as far as possible, strive to achieve backwards compatibility. All major browsers have already implemented escape and unescape; why would they un-implement them?

Browsers would have to redefine escapeand unescape if the new specification requires them to do so. But wait! The people who write specifications are quite smart. They too, are interested in not breaking backwards compatibility!

I realize that the above argument is weak. But trust me, ... when it comes to browsers, deprecated stuff works. This even includes deprecated HTML tags like <xmp> and <center>.

Using escape and unescape:

So naturally, the next question is, when would one use escape or unescape?

Recently, while working on CloudBrave, I had to deal with utf8, latin1 and inter-conversions.

After reading a bunch of blog posts, I realized how simple this was:

var utf8_to_latin1 = function (s) {
    return unescape(encodeURIComponent(s));
};
var latin1_to_utf8 = function (s) {
    return decodeURIComponent(escape(s));
};

These inter-conversions, without using escape and unescape are rather involved. By not avoiding escape and unescape, life becomes simpler.

Hope this helps.