| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Tuesday, June 9, 2009

Javascript URL Linker

As a part of the task management system that I'm working on, I wanted to be able to extract URLs from simple text strings and turn them into links without allowing users the ability to manipulate my HTML. After a bit of scouring the web via Google, W3Schools.com provided the answer:
function makeLink(str) {
var linkString = "";
var ary = str.split(" ");
var a=0;
while (a < ary.length) {
linkString += (ary[a].match("http")) ? ary[a].link(ary[a])+" " : ary[a]+" ";
a+=1;
}
return linkString;
}
This code will extract URLs from a string, turn them into links, and insert them back into the string in their original position.

Enjoy!

0 comments: