JSencodeURIComponent结果与FORM创建的结果不同
我认为浏览器对表单中输入的值进行了正确编码。
但这个简单的测试文件“test_get_vs_encodeuri.html”表明事实并非如此:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
</head><body>
<form id="test" action="test_get_vs_encodeuri.html" method="GET" onsubmit="alert(encodeURIComponent(this.one.value));">
<input name="one" type="text" value="Euro-€">
<input type="submit" value="SUBMIT">
</form>
</body></html>
当点击提交按钮时:
encodeURICompenent 将输入值编码为“Euro-%E2%82%AC”
而浏览器输入 GET查询只写一个简单的“Euro-%80”
有人能解释一下吗?
我如何使用 Javascript 以与浏览者的 FORM (windows-1252) 相同的方式对所有内容进行编码???(转义函数不起作用,encodeURIComponent 也不起作用)?
或者encodeURIComponent是否进行了不必要的转换?
I thought values entered in forms are properly encoded by browsers.
But this simple test file "test_get_vs_encodeuri.html" shows it's not true:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
</head><body>
<form id="test" action="test_get_vs_encodeuri.html" method="GET" onsubmit="alert(encodeURIComponent(this.one.value));">
<input name="one" type="text" value="Euro-€">
<input type="submit" value="SUBMIT">
</form>
</body></html>
When hitting submit button:
encodeURICompenent encodes input value into "Euro-%E2%82%AC"
while browser into the GET query writes only a simple "Euro-%80"
Could someone explain?
How do i encode everything in the same way of the borwser's FORM (windows-1252) using Javascript??? (escape function does not work, encodeURIComponent does not work either)?
Or is encodeURIComponent doing unnecessary conversions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个字符编码问题。您的文档使用的字符集 Windows-1252 其中
€
位于位置 128,使用 Windows-1252 编码为 0x80。但是encodeURICompenent
是期望输入为 UTF-8,因此使用 Unicode 的字符集,其中€
位于位置 8364 (PDF),使用 UTF-8 0xE282AC 编码。解决方案是对文档也使用 UTF-8。或者您编写一个映射以将 UTF-8 编码字符串转换为 Windows-1252。
This is a character encoding issue. Your document is using the charset Windows-1252 where the
€
is at position 128 that is encoded with Windows-1252 as 0x80. ButencodeURICompenent
is expecting the input to be UTF-8, thus using Unicode’s charset where the€
is at position 8364 (PDF) that is encoded with UTF-8 0xE282AC.A solution would be to use UTF-8 for your document as well. Or you write a mapping to convert UTF-8 encoded strings to Windows-1252.
我认为问题的根源在于字符编码。如果我在元标记中弄乱字符集并使用不同的编码保存文件,我可以让页面在浏览器中呈现,如下所示:
(来源:boogdesign.com)
那个
看起来很像您从encodeURIComponent 获得的内容。但是我找不到任何编码组合对encodeURIComponent 返回的内容有任何影响。我可以改变 GET 查询返回的内容。 这是您的原始页面,提交会给出一个网址例如:
这是该页面的 UTF-8 版本,提交会给出一个看起来像这样的 URL(在 Firefox 中):
但是如果我复制并粘贴它,我会得到:
所以看起来如果页面是 UTF-8,则 GET 和encodeURIComponent 匹配。
I think the root of the problem is character encodings. If I mess around with charset in the meta tag and save the file with different encodings I can get the page to render in the browser like this:
(source: boogdesign.com)
That
€
looks a lot like what you're getting from encodeURIComponent. However I could find no combination of encodings which made any difference to what encodeURIComponent was returning. I can make a difference to what the GET query returns. This is your original page, submitting gives an URL like:This is a UTF-8 version of the page, submitting gives an URL that looks like this (in Firefox):
But if I copy and paste it I get:
So it looks like if the page is UTF-8 then the GET and encodeURIComponent match.