Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
976 views
in Technique[技术] by (71.8m points)

javascript - After successful tweet, execute the callback + Twitter

I am doing the "share on Twitter" functionality. I have share button and when I click it will open a widget of twitter and showing me the share box. When I will press on Tweet button of widget , it will post my message on my twitter timeline.

now I want to handle a callback, when I tweet successfully, the pop up should not display and my website should redirect next page.

I refereed Is there a callback for Twitter's Tweet Button? this question but not working as per my requirements.

Twitter providing the events but it will execute on Tweet button which are embedded in our site. My tried with following code:

<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.pinceladasdaweb.com.br/blog/" data-via="pinceladasdaweb" data-lang="pt" data-text="Pinceladas da Web">Tweetar</a>

Js code:

window.twttr = (function (d,s,id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
js.src="https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
}(document, "script", "twitter-wjs"));

twttr.ready(function (twttr) {
twttr.events.bind('tweet', function () {
    alert('Tweeted!');
});
});

});

Is there a way to redirect to next page after successful tweet?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Generally there is no method or intent for a callback from a tweet. I used below hack to make a callback after a successful tweet:

window.twttr = (function (d,s,id) {
    var t, js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
    js.src="//platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
    return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
    }(document, "script", "twitter-wjs"));

$(document).on('click','.twitter',function(){

   var tweetUrl = 'https://twitter.com/intent/tweet?url='+window.location.href;
   var x = screen.width/2 - 700/2;
    var y = screen.height/2 - 450/2;
    var child = window.open(tweetUrl, "popupWindow", "width=600, height=400,left="+x+",top="+y);
       child.focus();
      var timer = setInterval(checkChild, 1);
      document.domain = 'example.com';     //Replace it with your domain
      function checkChild() {
        if(child.closed){
          clearInterval(timer);
        }
        if(child.window && child.window.closed){   //Code in this condition will execute after successfull post
          //Do your stuff here
          console.log('I am here after a successful tweet.');
          clearInterval(timer);
        }  
      }
});

<a class="twitter"  href="javascript:void(0)">Tweet</a>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...