scriptygoddess

08 Jun, 2004

Mozilla and IE decoder

Posted by: Jennifer In: Scripts

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

June 9th, 2004 at 12:08 am

Avatar

I fixed the bookmarklet.txt link.

2 | Jennifer

June 9th, 2004 at 3:04 pm

Avatar

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

3 | Lynda

June 11th, 2004 at 7:55 am

Avatar

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

June 12th, 2004 at 11:16 am

Avatar

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

5 | 99 shades of grey

August 18th, 2004 at 7:31 pm

Avatar

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

August 20th, 2004 at 8:35 am

Avatar

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

7 | Movalog

October 20th, 2004 at 6:41 am

Avatar

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….

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements