twitterAutoLinks: jQuery extension to replace twitter @names for links to their respective pages.

Save the following on a .js file, preferably named jquery.twitterAutoLinks.js
[javascript]
/** Auto replace all Twitter nicknames for links */
$.fn.twitterAutoLinks = function() {
return this.each(function() {
var html = $(this).html();
$(this).html(html.replace(/B@([w-]+)/gm, ‘<a href="http://twitter.com/$1" target="_blank">@$1</a>’));
});
};
[/javascript]

Usage

After importing the jquery.twitterAutoLinks.js file (make sure it’s after you’ve imported jquery itself), you’ll use the extension as follows.

[html]
<script type="text/javascript" language="Javascript">

$(document).ready(function() {
//say you’re running a wordpress blog and you want to do this for your posts
$("div.entry").twitterAutoLinks();
});

</script>
[/html]

One thought on “twitterAutoLinks: jQuery extension to replace twitter @names for links to their respective pages.

Leave a Reply

Your email address will not be published. Required fields are marked *