javascript 如何在javascript中解码url编码的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16645224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:29:38  来源:igfitidea点击:

How to decode url-encoded string in javascript

javascripturl-encoding

提问by Michael Schmidt

I use javascript / jquery to fill dom elements that contain umlauts:

我使用 javascript / jquery 来填充包含变音符号的 dom 元素:

var text1 = "Unser platonisches Internetreich droht in die H%E4nde einer bewaffneten Miliz zu fallen."

$("#quote1 span").html(unescape(text1));

How can I get rid of the url encoding e.g. "H%E4nde" and use "H?nde" instead? I tried

如何摆脱 url 编码,例如“H%E4nde”并改用“H?nde”?我试过

<meta charset="utf-8" />

<meta name="http-equiv" content="Content-type: text/html; charset=UTF-8"/>

<script type="text/javascript" src="js/index.js" charset="utf-8"></script>

But none of them seem to work...

但它们似乎都不起作用......

Thanks for help.

感谢帮助。

回答by Lepidosteus

That is not UTF-8, that is percent encodingalso known as url encoding.

那不是 UTF-8,即百分比编码,也称为 url 编码。

You can use decodeURIComponent()to convert it back before displaying it

您可以使用decodeURIComponent()在显示之前将其转换回来

$("#quote1 span").html(decodeURIComponent(text1));

$("#quote1 span").html(decodeURIComponent(text1));