Mozilla and IE decoder

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"), "&lt;");
strSelection = strSelection.replace(new RegExp(">","g"), "&gt;");
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"), "&lt;");
strSelection = strSelection.replace(new RegExp(">","g"), "&gt;");
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.

7 Responses to “Mozilla and IE decoder”

  1. Mark Says:

    I fixed the bookmarklet.txt link.

  2. Jennifer Says:

    Well the only browser I was able to get mine to work with on the Mac was Firefox… :\
    Damn Macs!!! ;-)

  3. Lynda Says:

    This is neat. I’ll need to think about adding this to my site. I’m in Mozilla on my mac and it works. I don’t understand why all the different browsers read javascript SO differently!

  4. lotta Says:

    would this be possible to add to the edit-form-advanced? I just don’t really get how to do it :D

  5. 99 shades of grey Says:
    Customising Quicktags II
    A quick update to the Quicktag customisation : I just incorporated Scriptygoddess’ decodeIt function, which neatly translates ‘ into ‘&gt;’. The original version of the function was IE-friendly only, however an update sees it working quite happ…

  6. Arvind Says:

    Super I managed to make this work with Quicktags :D Now I can encode within MT :D Weeee

  7. Movalog Says:
    Quicktags with 3.12
    With Movable Type 3.12, Six Apart built in a more powerful set of quicktags and on the whole they worked pretty well and looked quite nice too. I didn’t really want to go back to the clunky Quicktags of before….