最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.location.href.split('').pop() returning special characters instead of actual value - Stack O

programmeradmin3浏览0评论

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 ?

Share Improve this question asked Dec 4, 2017 at 9:05 Alexander SolonikAlexander Solonik 10.3k19 gold badges86 silver badges185 bronze badges 5
  • 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
Add a ment  | 

2 Answers 2

Reset to default 9

Use 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

发布评论

评论列表(0)

  1. 暂无评论