Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Automatic Link [IMPORTANT] Re: Automatic Link [IMPORTANT]

#93777
Mottie
Member

Hi Schart!

First off there are a bunch of errors on your page. I found four <head> tags on that page as well… there should only be one set of opening and closing tags.

So, I’m guessing you mean the “Recent Activity” section? I wrote a script a while back that replaces urls in a shout box with links and I got it to work on your site. Here is a demo.

It will replace any url it finds with a link, or you can label the url:

// http://www.google.com becomes
http://www.google.com

// label the url using curly brackets around the label {label}
// separate the label and url with a colon {label}:url
// {Google}:http://www.google.com becomes
Google

Here is the code

jQuery(window).load(function(){
var regex = /({(w+?)}:)?(https?://[w-.]+.[a-zA-Z]{2,3}(?:/S*)?(?:[w])+)/g;
jQuery('ul.feedList').find('li b').each(function(j){
var last = ' ', txt, shout = $(this);
shout
.html(function(i,h){
var txt, url = h.split(' ');
jQuery.each(url, function(j, u) {
if (regex.test(u)) {
txt = u.split(':')[0] || ' ';
if (txt.indexOf('}') >= 0) {
u = u.replace(txt + ':', '');
var x = 1; // find open bracket (spaces in link name)
while (txt.indexOf('{') < 0 && j-x > 0) {
txt = url[j-x] + ' ' + txt;
url[j-x] = '';
x++;
}
txt = txt.replace(/[{}:]/g, '');
} else {
txt = '';
}
url[j] = '' + ((txt === '') ? u : txt) + '';
}
});
h = url.join(' ');
return h;
});
});
});

If you can’t get it to work on your site, then it may be due to:

  • Javascript errors preventing the code below it from running
  • Multiple copies of jQuery; you only need one copy.