48

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

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

<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.

Lepidosteus's user avatar
Lepidosteus
12.1k4 gold badges41 silver badges51 bronze badges
askedMay 20, 2013 at 8:13
Michael Schmidt's user avatar
3

2 Answers2

77

That is not UTF-8, that ispercent encoding also known as url encoding.

You can usedecodeURIComponent() to convert it back before displaying it

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

answeredMay 20, 2013 at 8:15
Lepidosteus's user avatar
Sign up to request clarification or add additional context in comments.

Comments

26

Use either of the following built in JavaScript function to decode any encoded text. No need for jquery or any other library.

const text1 = "Unser platonisches Internetreich droht in die H%E4nde einer bewaffneten Miliz zu fallen."console.log(decodeURI(text1))console.log(decodeURIComponent(text1))

update:In case you're wondering how to encode, decoded text, you can use another built in JavaScript function like so

console.log(encodeURI(text1))console.log(encodeURIComponent(text1))
answeredOct 20, 2020 at 7:35
Chukwuemeka Maduekwe's user avatar

1 Comment

This answer can be improved by sharing the conditions under which one might want to use one decoder over the other.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.