0

I am working on a random quote machine and am trying to add a button that tweets the quote displayed on the screen. I can get it to work for basic quotes using encodeURI() but some of my quotes have special characters like brackets, colons, and semi-colons.

I've tried escape(), encodeURI(), and encodeURIComponent(), but none of these work with these characters. Here is what I have for the twitter button (content is the variable that contains the quote I want to tweet). How can I tweet quotes with these special characters?

$("#twitter-share").click(function(){
  window.open('https://twitter.com/intent/tweet?text="' + encodeURI(content) + '"', '_blank');
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • `encodeURIComponent()` is the only function you need. Describe the problems you have when using it. Forget the others, especially `escape()`. – Tomalak Mar 19 '17 at 10:46
  • If I have a quote that is "words words: words" it only includes "words words:" in the twitter URL. Same goes for brackets and semi-colons. – jmatty16 Mar 19 '17 at 10:50
  • Yes because you use `encodeURI()`, which is wrong. See duplicate thread, the answer is comprehensive. – Tomalak Mar 19 '17 at 10:51
  • Sorry I should have been more clear. I have tried encodeURIComponent() and while it worked for most of the characters, it leaves the apostrophes as ’ when I use the tweet button. Maybe there is something I'm missing. – jmatty16 Mar 19 '17 at 11:22
  • Maybe there is. Please make the smallest-possible code sample that reproduces the behavior and post it as a new question. Chances are that you are doing something wrong. The chance that there is a bug in the Twitter API (or in a core JS function) are infinitesimal in comparison. – Tomalak Mar 19 '17 at 16:27

0 Answers0