« Online IDE for Node.js | Main | Cosign with Django »

July 25, 2011

Link bare URLs in a HTML element

function link_em(da_id){

    try{ var tml = document.getElementById(da_id).innerHTML; } catch(e){ return false; }

    var reg = /[^href\=\"](http|https):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?\/?/gm;

    var list = [];

    list = tml.match(reg);

    var count = 0;

    for(i in list){

       if(list[i]){ if(list[i] != document.getElementById(da_id).innerHTML){ if( parseInt(list[i]) + '' != list[i]){

          count++;

          tml = tml.replace(list[i].replace(/\(/,''), '<a href="' + list[i].replace(/\(/,'') + '">' + list[i].replace(/\(/,'') + '</a>');

          }}}

       }

try{ if(list[0]){    

       document.getElementById(da_id).innerHTML = tml;

       } } catch(e){ var blah = ''; }

    }


Tags: JavaScript

Posted by pj at July 25, 2011 04:38 PM

Comments