Creating auto-clickable urls
In message boards and in the comment areas of websites, it's often possible for someone to post a comment and point to one or more urls. Unfortunately, most if not all websites expose rather rudimentary means to enter comments. As a result, the comments pointing to one or more urls lack the following :
The remainder of the article provides two ways to add auto-clickable urls to comments or any other text area. The first is a non-interactive technique to convert urls into clickable urls. The other is a full fledged interactive technique providing an elegant way to add clickable urls without being seen.
1. Automatically turning urls into clickable urlsIt may be invoked either when the user clicks a special command button, or simply when he submits a form. A javascript routine is executed, parses the content of the comment area using a regular expression, and replaces all urls by anchored urls. Anchored urls have the necessary html tags to ensure that users will be able to click on them. Of course, to avoid reentrance, the regular expression makes the difference between urls and already clickable urls. The code goes like this. Suppose we have a form where users type some content in a text area named body. The form has a submit button that when activated executes the following routine : <script language="javascript"> // // create auto-links. Replace any url to cliquable urls. All existing clickable urls are left as is. // we assume we have a form with a text area whose name is "body" // // S.Rodriguez. August 5, 2003. // // supported browsers : Netscape, Mozilla, Internet Explorer // function autolink(s) { var hlink = /\s(ht|f)tp:\/\/([^ \,\;\:\!\)\(\"\'\<\>\f\n\r\t\v])+/g; return (s.replace (hlink, function ($0,$1,$2) { s = $0.substring(1,$0.length); // remove trailing dots, if any while (s.length>0 && s.charAt(s.length-1)=='.') s=s.substring(0,s.length-1); // add hlink return " " + s.link(s); } ) ); } function doautolink() { var bodycontent = document.forms["message"].elements["body"].value; bodycontent = autolink(bodycontent); document.forms["message"].elements["body"].value = bodycontent; } </script> <!-- and now the form itself --> <form name="message" method="post" action="NewArticle.php?qs_submit=1" enctype="multipart/form-data"> <textarea name="body" rows="8" cols="60"></textarea> <input type="button" value="Autolink" onclick="doautolink()"> </form> This code automatically adds anchors to urls so they are made clickable. And works on both Netscape Mozilla and Internet Explorer. Notice that no text selection is required.
What now if we are willing to create anchors interactively? For instance, a standard way to link to some document is to have an url prepared in the clipboard, and then manually embed the anchor html tag to it :
2. Interactively turning urls into clickable urlsInteractively creating clickable urls is something much needed in the text area. It is not amazingly odd to introduce a dedicated button at this point, next to the submit button. We add a create link button which, when clicked, replaces the current selection in the text area with a full fledged hyperlink. In order to make things simple enough, the current selection in the text area is what will become the actual decorated text. So if you are say willing to add a link to a click here, just select those two words and click the button. Clicking the button executes javascript code, basically extracts the current selection and then replaces it with an anchor of the form We can actually take advantage of advanced Internet Explorer capabilities (from JScript, which is not standard javascript) and use the content of the clipboard to replace the AAAAAAAA placeholder as well with the actual hyperlink. To exemplify how this could work like a charm, just make sure to have an hyperlink in your clipboard. Thanks to that paste from the clipboard, the creation of hyperlink is really intuitive. The process can be sumed up as follows :
For the clipboard portion of code to work seamlessly, Internet Explorer must be allowed to paste content, which is the default setting. In case not, bring up the Internet Explorer options, Security tab, Internet Zone, and choose "Allow paste operations" in the options. Now for the actual code : <script language="javascript"> // // create an hyperlink on the fly, on the current text selection. // we assume we have a form with a text area whose name is "body" // // S.Rodriguez. August 5, 2003. // // supported browser : Internet Explorer // function createlink() { var sel = document.selection.createRange(); if (sel == null) return; // replace the text selection var szText = sel.text; szText = "<a href=\"PLACEHOLDER\" target=_blank title=\"Open in a new window\">" + szText + "[^]</a>"; sel.text = szText; // replace anchor placeholder with clipboard content var r = document.all["body"].createTextRange(); r.findText("PLACEHOLDER"); r.select(); r.execCommand("Paste"); // Internet Explorer only } </script> <!-- and now the form itself --> <form name="message" method="post" action="NewArticle.php?qs_submit=1" enctype="multipart/form-data"> <textarea name="body" rows="8" cols="60"></textarea> <input type="button" value="Create link" onclick="createlink()"> </form> The bonus track is that the hyperlink opens in a new browser window, without additional effort.
Stéphane Rodriguez - August 5, 2003.
|
Home Blog |