My objective is to achieve the following on click:
Share the URL and text on Twitter in a popup window
Receive a response from Twitter indicating whether the tweet was successful or failed
Close the popup window after the tweet is successfully posted or when the user chooses to close it
The challenges I am facing include:
Not receiving any response from Twitter regarding the success or failure of the tweet
The popup window remains open even after the tweet has been successfully posted
I have explored various solutions on Stack Overflow without success so far
The code below is within the HTML body tag:
<script>
window.twttr = (function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function(f) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
</script>
Function to open in a popup:
function share_to_twitter(sParamUrl, sParamText, iParamWinWidth, iParamWinHeight) {
var iWinTop = (screen.height / 2) - (iParamWinHeight / 2);
var iWinLeft = (screen.width / 2) - (iParamWinWidth / 2);
window.open('http://twitter.com/intent/tweet?url='+encodeURIComponent(sParamUrl) + '&text=' + sParamText,'top=' + iWinTop + ',left=' + iWinLeft + ',toolbar=0,status=0,width=' + iParamWinWidth + ',height=' + iParamWinHeight, '_blank');
}
share_to_twitter('https://example.com','Text to tweet', 640, 480);
Receiving response from Twitter and taking necessary action:
twttr.ready(function (twttr) {
twttr.events.bind('tweet', function(event) {
alert(event);
/*Example:
if(event == 'success'){
window.close();
}
else{
//Do something else
*/
});
});