Archive for April, 2010

Programmatically clicking an anchor tag via Javascript

Here’s a simple one that occurs often. Take for instance, the links on the sidebar of this blog. The links are wrapped in list items, however you’ll notice that you can just as easily click the big list item block as you can the link itself. Additionally, hovering over the list item triggers the hover state of the child link element. Here’s the code used to achieve this.

// Cause the parent list item to function like its child link
// Note we must use window.location as triggering a click event programmatically will not work!
$('li.widget_categories li, li.widget_archives li').click(function() {
    window.location = $(this).children('a').attr('href');
});

// When mousing over a list item, we add a class to the child link that imitates its hoverstate.
// Triggering mouse events and the like will only cause recursion errors.
$('li.widget_categories li, li.widget_archives li').mouseenter(function() {
    $(this).children('a').addClass('active');
});

// Same as above, only this time we return to normal
$('li.widget_categories li, li.widget_archives li').mouseleave(function() {
    $(this).children('a').removeClass('active');
});

Simple stuff here, but that’s about all I seem to ever have time to write down.

Adding social sharing buttons after page load

More and more sites are adding the various slew of social sharing buttons onto their pages and blog posts. There are several widgets like ShareThis and AddToAny that attempt to handle this, but what if you only want to add specific sharing buttons to your page? And what if you want to delay the loading of these buttons so as to not block the loading of your page?

I spent about an hour today solving this problem to implement a Digg, Facebook, and TweetMeMe combo of buttons. The first two weren’t much trouble, but the ReTweet button and its generated iFrame were causing me headache. My soluction simply added the iFrame myself, note also that in order to add javascript via jQuery, one must use a quirky workaround:

tweetmeme_style = 'compact';

function loadSocialButtons() {

     var str = '<ul>';

     // add digg
     str += '<li>';
     str += '<script src="http://widgets.digg.com/buttons.js" type="text/javascript">// <![CDATA[';
     str += '<';
     str += '/script>';
     str += '<a class="DiggThisButton DiggCompact"></a>';
     str += '</li>';

     // add facebook
     str += '<li>';
     str += '<a id="fb-share" name="fb_share" type="button_count" href="http://www.facebook.com/sharer.php" mce_href="http://www.facebook.com/sharer.php">Share</a>';
     str += '<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" mce_src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript">';
     str += '<';
     str += '/script>';
     str += '</li>';

     // add retweet
     str += '<li>';
     str += '<iframe width="90" scrolling="no" height="20" frameborder="0" src="http://api.tweetmeme.com/button.js?url=ADD_YOUR_URL&amp;style=compact" mce_src="http://api.tweetmeme.com/button.js?url=&amp;style=compact"></iframe>';
     str += '</li>';
     str += '</ul>';

     $('div#social').html(str);
}

$(document).ready(function() {
     setTimeout("loadSocialButtons()", 1000);
});

Make sure to add your page’s url in the TweetMeMe section, you can do this with some server side code.