tags:

views:

29

answers:

1

Hi all

I'm assigning a click event to a bunch of anchors by class name, and it works in all browsers except Firefox, here is the JS:

var click_addthis = function(e, href) {
   if (!e) {
      var e = window.event;
   }
   e.cancelBubble = true;
   if (e.stopPropagation) e.stopPropagation();

   window.open(href, "Share It", null);

   return false;
}

$(document).ready(function() {
   $(".addthis_button_facebook").click(function() { click_addthis(event, this.href) });
   $(".addthis_button_twitter").click(function() { click_addthis(event, this.href) });
});

Am I missing something? Thanks

+4  A: 

The problem area for Firefox is this section:

$(document).ready(function() {
   $(".addthis_button_facebook").click(function() { click_addthis(event, this.href) });
   $(".addthis_button_twitter").click(function() { click_addthis(event, this.href) });
});

You need to pass the event in from the handler so it's consistent, like this:

$(document).ready(function() {
  $(".addthis_button_facebook").click(function(e) { click_addthis(e, this.href) });
  $(".addthis_button_twitter").click(function(e) { click_addthis(e, this.href) });
});

You can also shorten it down to this since you're using the same function (return false stops propagation as well):

$(document).ready(function() {
  $(".addthis_button_facebook, .addthis_button_twitter").click(function() { 
    window.open(this.href, "Share It", null);
    return false;
  });
});
Nick Craver
My god I love this place, thank you! Oh, and I didn't realise return false stops propagration! Fantastic.
Mantorok
@Mantorok - Welcome :)
Nick Craver