Apply twitter-button javascript only when user clicks it


#1

Is there a relatively clean way I can get all of the built-in goodness of the simple javascript version of the twitter-button, but defer execution of its rather heavy-duty javascript until the user actually clicks on the button?

I’m thinking that I could build an anchor tag like this:

and then define a javascript function like:

function clicked_twitter_button( anchor_instance ) { // The line below is specified by twitter docs. !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
anchor_instance.onclick = null;
anchor_instance.href = "https://twitter.com/share";
anchor_instance.click();

}

If I make twitter_button.gif look exactly like the image that the twitter-specified javascript normally inserts, then execution of the twitter-specified javascript will essentially look like a no-op to the user.

The above works, but not like I want it to. Instead of popping up a tweet-window, it loads a tweet-page that replaces the page of my application. Furthermore, the data-url value is not carried into the tweet window.

Humbly, I would suggest that a technique like this should be used by default, if the tweet-count is not needed.

Thanks!


#2