-1

Possible Duplicate:
How to replace plain URLs with links?

So I've got a page with a bunch of URLs and no links. The page is constantly pulling in new information, so I can't add the tags myself. Where could I find a fast, lightweight, JavaScript snippet or jQuery plugin to linkify these URLs?

Turn http://blahblah.com/

into <a href="http://blahblah.com/">http://blahblah.com/</a>

6
  • 1
    I can foresee some regex coming. Commented Jul 20, 2012 at 2:39
  • Those functions in the solutions require the input of specific text, I need to be able to specify an element and have all of its contained text be changed. Commented Jul 20, 2012 at 2:44
  • 1
    Just pass the data received from the ajax to it before appending the linkified version? Or grab the element's html with .html() and replace it with the linkified version.. Commented Jul 20, 2012 at 2:45
  • THis yields no dice: replaceURLWithHTMLLinks($('li').text()) (regarding the first solution here: stackoverflow.com/questions/37684/…) Commented Jul 20, 2012 at 2:47
  • $('li').html(replaceURLWithHTMLLinks($('li').html())) or, if you have more than one li, $('li').each(function() { $(this).html(replaceURLWithHTMLLinks($(this).html())); }); Commented Jul 20, 2012 at 2:51

1 Answer 1

1

With JQuery:
Assuming your links are in p (for performance, use the closest selector)

$(document).ready(function(){ $('p').each(function(){ var content = $(this).html(); content = content.replace(/[^"=](http:\/\/\S*)/ig, '<a href="$1">$1</a>'); $(this).html(content); }); });​ 

Surely not the most optimized way, and the regex is not really well-formed but at least it doesn't replaces URLs that are in tags attributes (like images src).

Sign up to request clarification or add additional context in comments.

1 Comment

Regexps are brittle and should be avoided. I've summarized the best autolinkification libraries in another answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.