问题描述
escape()/ unescape()和encodeURI()/ decodeURI()都可以正常工作在我的浏览器中。
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
안녕하세요
然而,。
尽管encodeURI()和decodeURI()与上述utf-8字符串一起工作,文档(以及函数名称本身)告诉我这些方法是用于URI的;我没有看到任何地方提到utf-8字符串。
简单的说,对utf-8字符串使用encodeURI()和decodeURI()可以吗?
嗨!
当涉及到 escape
和 unescape
,我按两个规则生活:
- 避免他们你很容易就可以。
- 否则,使用它们。
如问题所述, escape
和 unescape
已被弃用。一般来说,应该避免使用不推荐的功能。
所以,如果 encodeURIComponent
或 encodeURI
为您做点窍门,您应该使用而不是 escape
。
使用他们当你不能轻易地避免他们:
浏览器将尽可能努力实现向后兼容性。所有主流浏览器已经实现了 escape
和 unescape
;为什么不执行它们?
浏览器必须重新定义 escape
和 unescape
如果新的规范要求他们这样做。可是等等!编写规格的人很聪明。他们也有兴趣不违反向后兼容性!
我意识到上述论据很弱。但是相信我,...当谈到浏览器时,弃用的东西有用。这甚至包括不推荐的HTML标签,如< xmp>
和< center>
。
使用 escape
和 unescape
:
所以自然地,下一个问题是,何时使用 escape
或 unescape
?
最近,在工作时,我不得不处理 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:
- Avoid them when you easily can.
- 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 escape
and 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.
这篇关于在JavaScript中对utf-8字符串使用encodeURI()与escape()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!