Search code examples
javascriptjquerytwitter

Tweet something based on JavaScript variable


I have my Codepen which can be found here (https://codepen.io/Mortiferr/pen/repRgX)

I have a button like so:

<button onclick ="tweetIt()" class="twitter" href="#twitter"><i class="fa fa-twitter fa-lg"></i> Tweet Quote</button>

It is a random quote machine that generates a random quote from an API. I want to make it so that the user can tweet out the quote on the screen. I have a variable called quote and author so I need to somehow use those to send out the tweet.

Thanks.


Solution

  • Here's how I did it:

    $('.twitter').on('click', function() {
      makeTwitterPage('https://twitter.com/intent/tweet?text=' + encodeURIComponent('"' + quote + '" ' + author));
    });
    function makeTwitterPage(url){
      window.open(url, 'Share', 'width=700, height=500, toolbar=0, scrollbars=1 ,location=0 ,statusbar=0,menubar=0, resizable=1');
    }