Tweet quote content on click

时间:2016-07-11 20:38:51

标签: javascript jquery twitter

I've been trying to find how to do this for hours... Here's my quote machine. I'm trying to get it to tweet the quote displayed when the twitter button is clicked, but right now all I can get it to do is go to the normal twitter share page by this code:

$('#tweet').click(function(){
        window.open("https://twitter.com/share");
    });

Thank you!

2 个答案:

答案 0 :(得分:1)

You can call the following link in your window.open statement:

 window.open("https://twitter.com/intent/tweet?text="+ $('#main-quote').text() +"&via=your-app-name&original_referer=your-url");

where:

  • text is the text to be posted and
  • via is the optional twitter profile you want to inform in the tweet

Your fiddle in action forked to : https://jsfiddle.net/claykaboom/qyqykdvm/

More info at: https://dev.twitter.com/web/tweet-button/web-intent

答案 1 :(得分:1)

Take a look into Twitter's javascript API

https://dev.twitter.com/web/javascript/creating-widgets

Here's a fiddle: https://jsfiddle.net/k0p7bc2j/ using createShareButton:

twttr.widgets.createShareButton(
  ' ',
  document.getElementById('new-button'),
  {
    count: 'none',
    text: document.getElementById('quote').value
  }).then(function (el) {
    console.log("Button created.")
  });