I have the following url:
gr-dev.indegene/el-gr/ο-πόνος/τι-είναι-ο-πόνος;
The problem is when i do:
window.location.href.split('/').pop()
I get a bunch of special characters , such as the below:
"%CF%84%CE%B9-%CE%B5%CE%AF%CE%BD%CE%B1%CE%B9-%CE%BF-%CF%80%CF%8C%CE%BD%CE%BF%CF%82;"
I just want to get τι-είναι-ο-πόνος
instead of the above , how do i do that ?
I have the following url:
gr-dev.indegene./el-gr/ο-πόνος/τι-είναι-ο-πόνος;
The problem is when i do:
window.location.href.split('/').pop()
I get a bunch of special characters , such as the below:
"%CF%84%CE%B9-%CE%B5%CE%AF%CE%BD%CE%B1%CE%B9-%CE%BF-%CF%80%CF%8C%CE%BD%CE%BF%CF%82;"
I just want to get τι-είναι-ο-πόνος
instead of the above , how do i do that ?
-
I get a bunch of special characters
- no, they look like the characters you should be getting – Jaromanda X Commented Dec 4, 2017 at 9:07 - Use window.decodeURI(), – Manoj Jadhav Commented Dec 4, 2017 at 9:08
- @JaromandaX, thanks can you elaborate ? – Alexander Solonik Commented Dec 4, 2017 at 9:11
- @JaromandaX, how e this works fine , with the english language and not foreign languages ? – Alexander Solonik Commented Dec 4, 2017 at 9:14
- because puter languages are naturally biased towards the spoken language of their creators ... i.e. English – Jaromanda X Commented Dec 4, 2017 at 9:35
2 Answers
Reset to default 9Use the below code.
window.decodeURI(window.location.href.split('/').pop());
Please refer to decodeURI
var decodedVal = window.decodeURI("%CF%84%CE%B9-%CE%B5%CE%AF%CE%BD%CE%B1%CE%B9-%CE%BF-%CF%80%CF%8C%CE%BD%CE%BF%CF%82;");
console.log(decodedVal);
You are getting the correct value. With one problem, you are getting the encoding version of this value. So you need to decode.
You can do that like:
window.decodeURI(window.location.href.split('/').pop())
to get the decoded version of the string.
More Info:
URLs only uses specific allowed characters like alphabetic characters, numerals and a few special characters that have a meaning in the URL string. So, any other character should be encoded so that they don't cause problems.
The string τι-είναι-ο-πόνος
is not a normal character, so is treated as a special character, because of that needs to be encoding to "%CF%84%CE%B9-%CE%B5%CE%AF%CE%BD%CE%B1%CE%B9-%CE%BF-%CF%80%CF%8C%CE%BD%CE%BF%CF%82;"
So to get back the original string you need to decode as shown above