After a successful tweet, call callback + Twitter

I am using the "share on Twitter" functionality. I have a sharing button, and when I click on it, it will open the twitter widget and show me the field for sharing. When I click on the “Tweet” button of the widget, it will post my message on my twitter timeline.

Now I want to handle the callback, when I twist successfully, the popup should not display, and my site should redirect the next page.

I refereed Is there a callback for the Twitter button Twitter? this question, but does not work according to my requirements.

Twitter, which provides events, but it will be executed on the Tweet button, which are built into our website. I tried the 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 the next page after a successful tweet?

+1
source share
1 answer

Typically, there is no method or intention to call from a tweet. I used the below hack to make a callback from a 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.closed){ //Code in this condition will execute after successfull post //Do your stuff here clearInterval(timer); } } }); <a class="twitter" href="javascript:void(0)">Tweet</a> 
+2
source

Source: https://habr.com/ru/post/897828/


All Articles