Percent encoding in window.location

2k views Asked by At

When I open a url with special characters using window.location, it seems to percent encode the special characters and then opens the URL. For example

var url = "http://gramfeed.com/instagram/tags/kühl";
window.location = url;

This will result in opening a page with URL:

http://gramfeed.com/instagram/tags/k%C3%BChl

instead of:

http://gramfeed.com/instagram/tags/kühl

How do I make the URL open correctly without percent encoded characters

Here is a jsfiddle to play with the code: http://jsfiddle.net/krisrak/aSkMR/

2

There are 2 answers

2
Yuriy Galanter On BEST ANSWER

I do not believe the problem is with windows.location and your JavaScript. The problems is rather with how gramfeed.com interprets tags. Try this in your code:

var url = "https://www.google.com/search?q=kühl"
window.location = url;

See that special characters stay unconverted.

Now try typing http://gramfeed.com/instagram/tags/kühl directly in browser address bar - the URL gets converted.

0
sidonaldson On

I also came across this issue but it was an entirely different problem, although the symptoms were the same. In the end it turned out I was redirecting to a desktop website URL but for mobiles this got redirected on their server to their mobile site and it was then that it got encoded twice.

So it's always worth trying to redirect to the mobile site directly if possible and on mobile.

Hope this helps someone else :)