473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Converting URLs to clickable hyperlinks using Javascript

I have a piece of text that could have http://somelink.com in it. I
want to convert every potential hyperlink to an actual link in HTML,
like <a href='$link'>$link</a>

What is the best way to do this in Javascript?

Jan 18 '07 #1
2 4214
ASM
Jason a écrit :
I have a piece of text that could have http://somelink.com in it. I
want to convert every potential hyperlink to an actual link in HTML,
like <a href='$link'>$link</a>

What is the best way to do this in Javascript?
Don't know what is the best ... you could try :
onload = function() {
var L = document.links;
// or
// L = document.getElementsByTagName('A');
for(var i=0; i<L.length; i++)
{
L[i].href = 'http://somelink.com';
if(L[i].text) { L[i].text = 'my link'; }
else L[i].innerHTML = 'my link';
}
}
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jan 18 '07 #2
Jason wrote:
I have a piece of text that could have http://somelink.com in it. I
want to convert every potential hyperlink to an actual link in HTML,
like <a href='$link'>$link</a>
var x = document.getElementById('myDivWithLinkText');
x.innerHTML = x.innerHTML.replace(/(http:\/\/[^ ]+)/g,'<a
href="$1">$1</a>/');

Make sure your text doesn't contain actual links, or it will screw them up.
If your case is more complicated than the simplest possible case, then you
would need to expand on this solution.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jan 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sergio Otoya | last post by:
Hi all, Is there any way of copying a file using javascript, not using the Filesystemobject (ActiveX). I need this to run in Windows and MACS. Any help would be greatly appreciated. Thanks...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
6
by: prash.marne | last post by:
hi all , this is a very simple problem but i need some help, My situation is : i have a form which opens in a pop-up window , in which user will insert his email-id & name , there are two...
7
by: kmitchell00 | last post by:
I am coding in Coldfusion MX7 and using Javascript for some of the functionality. The basic functionality I'm coding is, based on a value the user chooses from a dropdown box, I populate address...
3
by: akristensen | last post by:
I am new to this site, so be patient if I do not ask the question correctly. Current Target Platform: Browser: MS IE, script language: Javascript (will use VBScript, but JS is preferred), External...
15
manuelgk
by: manuelgk | last post by:
Hi everybody, I'm a total beginner using Javascript and I would like to know how can I get in a variable the url of hyperlinks? I want to do something like a status bar of mozilla (not a bar) but...
2
kaleeswaran
by: kaleeswaran | last post by:
hi! i am comparing two dates.so i am getting one date is from my data base.and i send it that value using request parameter.so i am converted date to string .now i want to convert that string...
1
by: =?Utf-8?B?amVmZjMwNDg=?= | last post by:
Hi, I want to incorporate a JavaScript routine (found on the internet) in my page that will restore the scrollbar position of a div tag on postback. I am using master pages which complicates...
1
by: srinivasreddya | last post by:
Hi All, How to convert UTF-8 text into Shift-JIS (Hex) format. I am able to convert UTF-8 to Unicode (Hex) (名 to Unicode (Hex): 0540D). And not able to convert to 名 to Shift-JIS (Hex): 96BC....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.