Percent encoding in window.location
Asked Answered
T

2

6

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/

Tavia answered 26/8, 2013 at 17:38 Comment(1)
percent encoding was caused in python server code, fixed in javascript by using decodeURIComponent and replacing in window.location.hashTavia
I
3

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.

Ibadan answered 26/8, 2013 at 21:54 Comment(2)
My bad, i think you are right. I'm converting and redirecting url in python code, not sure why this is percent encoding special characters: url = self.request.path.replace('/'+TAG_NAME, '#'+TAG_NAME) self.redirect(url)Tavia
fixed in javascript with decodeURIComponent: var tag_name = decodeURIComponent(window.location.hash.substring(1)); window.location.hash = tag_name;Tavia
F
0

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 :)

Fic answered 16/8, 2016 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.