Tom Lehman
Tom Lehman

Reputation: 89313

Javascript function to turn URLs and email addresses into clickable links

Is there a Javascript library that exposes a function to automatically turn URLs and email addresses into clickable links? Basically I'm looking for something that duplicates the Rails helper auto_link().

Upvotes: 1

Views: 646

Answers (2)

Darko
Darko

Reputation: 38880

EDIT: My bad, I totally misread/misunderstood the question. Left original answer for informative purposes.

Not off the top of my head. However it doesn't sound too hard to write one yourself:

function UrlToLink(url) {
    // 1. check if url IS a url or email using a regex
    // 2. if it isnt return null/false
    // 3. create a DOM `<a></a>` element and populate the href attribute with url (mailto:url if email) and the inner text
    // 4. return element
}

Usage:

document.appendChild(UrlToLink("http://a.url.com/"));

Finding regex's to match url's and email addresses isnt very hard ex: url, email

Upvotes: 0

Sampson
Sampson

Reputation: 268414

Autolink may be what you're looking for.

Upvotes: 8

Related Questions