Big problem with ' in URL

Hi all!
I’m try resolve task with Random Quote Machine and i have problem.
Than i generate href attribute for twitter link (post quote to tweet) and in quote exist " " symbol in the URL his transfor to ’ code and my URL crash. How fix it?

Sorry for my bad english.

Use encodeURIComponent().

Can anyone pls help me with my Random Quote machine ?
Here is link to my pen :

It’s bad idea because then text look’s like this “& #8217;s a sign you aren& #8217;t doing anything very innovative.”

Why this construction replace only first ’ ??

var str = a[0].content.slice(3, -5).replace("’", ‘%27’);

Can you post a link to your project?

Yes. sorry.

I can fix this bug then i add in replace-function RegExp with key gi.

$(".fa-twitter").attr(“href”, “” + str.replace(/’/gi, “%27”) + a[0].title);

Now in tweet i have normal string

OK, I’ve looked into this more, and it turns out there are several interesting/relevant things here:

  1. I stand by my recommendation — use encodeURIComponent() to encode strings to be used as values for parameters in URLs, because it’s designed for that specific purpose and it will catch problematic characters like ? and &.
  2. 'Straight single quotes' don’t actually need escaping in URL strings and will have no adverse effect (even though the official standard, RFC 3986, lists them as reserved characters).
  3. Curly single quotes, as with most other characters, should be escaped.

However, the root of your problem has nothing to do with any of this. The problem here is that the Quotes on Design API is returning strings that have already been optimized for HTML, meaning they contain HTML entities, including our friend ’ that’s causing you all this heartache.

Basically, you’ll need to find a way to unescape the HTML entities before encoding as a URI component. This should be elementary if you just want to deal with ’, and a little more involved if you want to catch every possible numerical entity.

1 Like

ok, i’m under stand!
Thank’s very much. I’m try to catch all HTML entities with loop and switch\case construction, i tihnk this simple and effective way.

Actually, I just found a sneaky workaround that’s both elementary and comprehensive:




1 Like

OMG =)
It’s work!!


No worries. I also learned a lot of new stuff! :grin:

1 Like