Home » "Build Backlinks"

Anchor Text Example

18 March 2009 No Comment

Anchor Text Example
using JavaScript, how do i get a link out of selected text?

I would like to know how to get the URL of a selected hyperlink to be display in a msgbox using javascript.

Best example i can give is you have a list of links on a webpage and a single button with the function i need attached, you highlight/select text on one link, and click a button on the page, the JavaScript creates an Alertbox and tells you what the URL is.

The links on the list are anchor tags surrounding a descriptive term for example function ExtractURLs () { // If re is corrupted: var re = /(ht|f)tp(s?)://[0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*(:(0-9)*)*(/?)([a-zA-Z0-9-.?,'/\+&%$#_]*)?/g;

// Then uncomment these lines and stick them together without any spaces: // var re = /(ht|f)tp(s?)://[0-9a-zA-Z] // ([-.w]*[0-9a-zA-Z])*(:(0-9)*)* // (/?)([a-zA-Z0-9-. // ?,'/\+&%$#_]*)?/g;

// For example: // A // B // C // makes ABC

var textBox = document.getElementById ("inputText");

var urls

var found = false; var urlText = "";

while ((urls = re.exec (textBox.value)) != null) { found = true; urlText += urls [0] + "n"; }

if (found) { alert ("URLs:n" + urlText); } else { alert ("No URL found."); } }


This simple demo extracts all URLs found in the text area provided below.


Many, if not all of the links on this website are affiliate links which means that we will be compensated if you choose to buy at some point in the near future. Please accept our sincere thanks for your support.