Sunday, October 26, 2008

AddThis - It's a Post Title Apostrophe Problem



Problem: Posts which contain an apostrophe don't work when you try to bookmark them with AddThis.

Reason: Any post with an apostrophe or single quote within the title causes a problem with the AddThis Javascript. Basically, the title of a post is stored as a Javascript string variable. If there is an apostrophe or single quote in the middle of the variable, it generates an error related to unterminated strings. On my page, it displayed an incorrect post for the bookmark. Unfortunately, it is not so uncommon to want to use apostrophes.

Solution:

This is the code from AddThis:


addthis_url='<data:post.url/>';
addthis_title='<data:post.title/>';
addthis_pub='your_account_name';


First, change the single quotes around '<data:post.title/>' to double quotes as follows:


addthis_title="<data:post.title/>";



Then add the following line of code:

addthis_title=addthis_title.replace(/\'/g,'');



This approach removes the apostrophe in the title string. It will still display in your post, of course, but will be removed in the title for the bookmark.

CAUTION: Just be sure not t0 use any double quotes in your blog titles.

Example: There is an apostrophe in the title of this post. Use the AddThis button below and you can see how this works.



Source: OpenLinkz provided the solution to this problem.

No comments: