FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Javascript to make text links into clickable links?

 


Dougie1
Is there javascript that makes every text link eg:
Code:
www.youtube.com

Into a proper link. Thanks.
jabapyth
sure
you'll need a good regex to find links, for example:
Code:
(?<=[^\"])(http://)?.+?\.[^s]+?\.(com|net|org|biz|co\.uk)(/.+?)?(?=[^\"])

you should modify this (if you know how) to suit your own needs

then, use
Code:

myregex=/(?<=[^\"])(http://)?.+?\.[^s]+?\.(com|net|org|biz|co\.uk)(/.+?)?(?=[^\"])/
function rep(e){
    if (e.substr(0,7)!="http://"){
        url="http://"+e
    }
    else{url=e;}
    return "<a href='"+url+"'>"+e+"</a>"
}
document.body.innerHTML=document.body.innerHTML.replace(myregex,rep)
jabapyth
that url regex was just a quickie i whipped up. Use Google to find a better one
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.