Random Quote Generator - Twitter intent breaks on special characters

Hi, I’m trying to make a random quote generator and I have problem with the Twitter part.
I’m using the twitter intent address and pass encoded text to it. When the quote has special characters in it (I mean quotation mark ("), apostrophe(’) or semi-colon(;)), resulting tweet intent only records the part before the characters I mentioned.
URL passed to Twitter seems fine. I even looked at example website to see how it passes the text to Twitter and it uses the exact same method, yet it works fine there. I’m not sure what’s the problem here, maybe I’m missing something obvious. Link to codepen - https://codepen.io/brikp/pen/NYEJOy.

I’m not sure why your code doesn’t work but this is how I did mine:

    $('#tweet-this').click(function() {
      var tweetJoke = $(".message").text();
      if (tweetJoke != 'Press below for a joke') {
        window.open('https://twitter.com/intent/tweet?text="' + tweetJoke + '"', '_blank');
      };
    });
1 Like

I used .text() method as you suggested and modified it a bit and now it works!. I think it was some kind of encoding issue, maybe replacing quotes with %27 (url friendly) could also work, I may try it tomorrow out of curiosity.

var actionValue = "https://twitter.com/intent/tweet?text=" + encodeURIComponent($("#message").text());
$('#tweetButton').attr('href', actionValue);

Thank you for your input :).

1 Like