Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/tangible/public_html/textpattern/lib/txplib_db.php on line 14
Tangible: Snippet: jQuery Linkifier
Mar 20 2008

Snippet: jQuery Linkifier

Here’s a very basic jQuery linkification snippet that I put together recently.

$(document).ready(function() { var linkable_content = $('div#linkable').html().replace(/(https?:\/\/[^ ;|\\*'"!,()<>]+\/?)/g,'<a href="$1">$1</a>'); $('div#linkable').html(linkable_content); });

Now, this can’t be used in all circumstances. It doesn’t check to make sure that you’re not trying to make a link out of the href in a pre-existing link, so it should only really be used when you’re sure that it’s just plain text that you’ll be working with.

I could have done this server-side, but using jQuery to linkify the URLs prevents search engine bots from following the links, which got around the problem I had of wanting some links followed and others ignored.

Updating this snippet to work with a class (.linkable) instead of an id (#linkable), and to include ftp:// URLs is left as an exercise for the reader. ;)

  1. I stumbled on your blog today while trying to solve a css issue. You have written some interesting and helpful content here. I also noticed on your about page that you studied Cognitive Science, and was wondering if you had considered writing about that too?

    Btw, I think if you add

    to the beginning of your regex, it will take care of the recursive href replacement issue.

    joshvf    Apr 23, 02:01 PM    #
  2. unless you also want to prevent search engines from indexing the linked content, would the nofollow attribute help?

    Sridhar Ratnakumar    Feb 27, 01:53 PM    #

Add a Comment

  Textile Help

<< I <3 my EeePC

Firefox 3 - Quick Impressions >>