Everything is there in the title itself. I am trying to create a custom tweet button with callback. I have pasted the code below. I am not using the Twitter Button code from twitter as I have a multiple tweet buttons on the same page and I want to pass a unique id to each callback according to which button was clicked.
First time the button is clicked, the popup opens for tweet and after tweeting, window closes but callback function is not called. Second time I click the same button (without reloading the page), the callback function is called! Any I idea why this is happening and how this can be corrected to work on first click?
Thank you all.
<a href="#" target="_blank" id="twtShareLink"
onclick="return twOpenPopup('<URL>','<TEXT>','<UNIQUE_ID>')"
>Tweet It!</a>
JS:
function twOpenPopup(url,text,unique_id){
twttr.events.bind('tweet',function(event){
alert('Here');
//Code that uses unique_id here;
});
var twturl="https://twitter.com/intent/tweet?url="+encodeURIComponent(url) +
"&count=none&source=tweetbutton&text=" + encodeURIComponent(text) + "&original_referer=" + encodeURIComponent('<?= $_SERVER['HTTP_HOST'] ?>');
document.getElementById("twtShareLink").href = twturl;
return true;
}
Because you’re not binding your event until you click on the button. You need to bind the event outside the
twOpenPopup()function.