History: I had previously written this decoder, but it wasn't compatible with anything but IE. (A frustrating point since I've been using Firefox pretty religously). I saw that Mark had written one compatible with mozilla – but the only one there convereted EVERYTHING (making it difficult to go back an edit anything you decoded). I also saw Alex had published his quick tags. So I took a little bit of all of them and created the following script. It can't be turned into a bookmarklet – because it requires having the fieldname passed to it – but at least I could add back the "decoder" button for comments here. (Although – Mark updated his post with the bookmarklet that will just encode brackets, etc.)
So here's the javascript:
(This needs to go inside <script language="JavaScript" type="text/JavaScript"> </script> tags)
function decodeIt(textfield) {
strSelection = ""
if (document.selection) {
strSelection = document.selection.createRange().text;
strSelection = strSelection.replace(new RegExp("<","g"), "<");
strSelection = strSelection.replace(new RegExp(">","g"), ">");
document.selection.createRange().text = strSelection;
}
//MOZILLA/NETSCAPE support
else if (textfield.selectionStart || textfield.selectionStart == '0') {
textfield.focus();
var startPos = textfield.selectionStart;
var endPos = textfield.selectionEnd;
strSelection = textfield.value.substring(startPos, endPos)
strSelection = strSelection.replace(new RegExp("<","g"), "<");
strSelection = strSelection.replace(new RegExp(">","g"), ">");
textfield.value = textfield.value.substring(0, startPos) + strSelection + textfield.value.substring(endPos, textfield.value.length);
}
}
You could then call this script either using a button like this:
<input type="button" onclick="decodeIt(document.FORMNAME.TEXTAREANAME)" value="Decode it">
or in a simple link like this:
<a href="javascript:decodeIt(document.FORMNAME.TEXTAREANAME)">Decode it</a>
I've only tested this on IE6 and Firefox on the PC (winXP). When I get access to a mac or more browsers, I'll test there too. This is the same script I have running for comments here – so if you want to test it on your local set up, please do – and let me know how/if it works.