Create a JavaScript Only “Tweet This” Button

14 Feb

Earlier I posted a “Tweet This!” button created with JavaScript and HTML. In this post I will show you how to create a button with nothing more than JavaScript. Just copy and paste this wherever you want the button.

<script type=”text/javascript”>

var twtTitle = document.title;

var twtUrl = location.href;

var maxLength = 140 – (twtUrl.length + 1);

if (twtTitle.length > maxLength) {

twtTitle = twtTitle.substr(0, (maxLength – 3))+’…’;

}

var twtLink = ‘http://twitter.com/home?status=’+encodeURIComponent(twtTitle + ‘ ‘ + twtUrl);

document.write(‘<a href=”‘+twtLink+'” target=”_blank”‘+’><img src=”tweetthis.jpg” border=”0″ alt=”Tweet This!” /’+’><‘+’/a>’);

</script>

The only thing you need to change is the image source. You may have your own image you would like to use or you can simply style the link with CSS.

Leave a comment