[Chicago] make some text a link via css or javascript

JS Irick hundredpercentjuice at gmail.com
Thu May 14 21:20:20 CEST 2009


> Regular expressions are built into JavaScript. Here's an example that will
> do what you want.
> var re = /ticket#(\d+)/g
> var text = "See ticket#123 and ticket#124"
> text.replace(re, '<a href="http://example.com/trac/ticket/$1>ticket#$1</a>')


Tsk, tsk spent too much time making the mockup and got beaten to it.

The one thing to add is to use innerHTML, and call the function in the
body's onLoad() method.

<head>
<SCRIPT type="text/javascript">
function replacer(){
   var re = /ticket#(\d+)/g
   document.getElementById("area").innerHTML=document.getElementById("area").innerHTML.replace(re,
'<a href="http://example.com/trac/ticket/$1>ticket#$1</a>')
}
</SCRIPT>
</HEAD>
<body onLoad="replacer()">
<DIV ID="area">
I'd like to point your attention to ticket#12
</DIV>
</body>


More information about the Chicago mailing list