BotanMan
BotanMan

Reputation: 1417

Google plus url sharing

I use this link for sharing my page via google plus.

 https://plus.google.com/share?url=http%3A%2F%2Fexample.com%2Fcompany%20name%2search

so I use encodeURIComponent for getting this url (also tried to use encodeURI). but when sharing box appears link is incorrect. It is:

 http://example.com/company

Forsure I can change ' ' with, for example, '-' (or another symbol), but I use value from url in my AngulaJs app for correct initializing and it will be better to use native way for this.

What is my fault or are there any native ways to fix this?

Upvotes: 0

Views: 358

Answers (1)

abraham
abraham

Reputation: 47833

%20 is an encoded space which is not a valid URL character. The most commonly used character instead of a space is a -.

The best approach would be to normalize the input into an ID that you will use to query your own database. E.g. Bob's Bar & Grill would get processed to bobs-bar-grill and then you would query your database to find:

{
    "id": "bobs-bar-grill",
    "name": "Bob's Bar & Grill"
}

This both makes your URLs clean and better protects you from a security perspective of random user content in URLs.

As a last resort you could double encode the path so that Google still has encoded spaces after it decodes the URL once.

var url = "http://example.com/company name";
encodeURI(encodeURI(url));
// "http://example.com/company%2520name"

And Google will send users to http://example.com/company%20name

Upvotes: 1

Related Questions