Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode periods for URLs in Javascript?

The SO post below is comprehensive, but all three methods described fail to encode for periods.

Post: Encode URL in JavaScript?

For instance, if I run the three methods (i.e., escape, encodeURI, encodeURIComponent), none of them encode periods.

So "food.store" comes out as "food.store," which breaks the URL. It breaks the URL because the Rails app cannot recognize the URL as valid and displays the 404 error page. Perhaps it's a configuration mistake in the Rails routes file?

What's the best way to encode periods with Javascript for URLs?

like image 657
Crashalot Avatar asked Feb 08 '11 21:02

Crashalot


People also ask

How do you escape the period of a URL?

Just add a \ before the period. Something like: \.

What does %20 replace in URL?

URL Encoding (Percent Encoding) URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.


1 Answers

I know this is an old thread, but I didn't see anywhere here any examples of URLs that were causing the original problem. I encountered a similar problem myself a couple of days ago with a Java application. In my case, the string with the period was at the end of the path element of the URL eg.

http://myserver.com/app/servlet/test.string

In this case, the Spring library I'm using was only passing me the 'test' part of that string to the relevant annotated method parameter of my controller class, presumably because it was treating the '.string' as a file extension and stripping it away. Perhaps this is the same underlying issue with the original problem above?

Anyway, I was able to workaround this simply by adding a trailing slash to the URL. Just throwing this out there in case it is useful to anybody else.

John

like image 191
John Rix Avatar answered Sep 19 '22 05:09

John Rix