View Full Version : If text selected / else if text not selected function

01-07-2004, 04:18 AM
hello, i have a serious problem with people who are not familiar with HTML and do not know how to create a link. i have this script that allows you to write a text, then select it, and push a button to fill in the URL and then the link-code gets filled into people's comments.

function ahrefThis() {
strSelection = document.selection.createRange().text
if (strSelection == "") document.comments.text.focus()
strHref = prompt("Enter the URL of the site to link:","http://")
document.selection.createRange().text = "<a href=\"" + strHref + "\" target=\"_blank\" class=\"text\">" + strSelection + "</a>"
if (strHref == null) return;

but i need a script that creates a link that is named "link" by default, too, like this:

<a href="URL">link</a>

also if no text is selected.
i want to offer both choices:

if text is selected use the selected text as the name of the link.
else use the word "link" to name the link automatically.

i hope that explains it.