views:

30

answers:

1

If I try to execCommand("justifycenter"... a paragraph on my page in Firefox, it doesn't work; it's giving me this crazy error:

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: editor.php?id=new :: onclick :: line 1" data: no]

I already know the reason; it's because my body tag doesn't have contenteditable on, which is annoying because the other browsers don't require this.

Now, my problem is I don't want the entire page to be editable. I'm writing something of a WYSIWYG page builder, and it's too much of a compromise to stick the entire editor into an iframe. Does anyone know any other alternatives? This whole execCommand stuff is new to me, and enabling it for the body, but disabling it for everything else seems like a really bad hack.

+1  A: 

Something like:

function myExecCommand(aCommandName, aShowDefaultUI, aValueArgument) {
   document.contentEditable = true;
   document.execCommand(aCommandName, aShowDefaultUI, aValueArgument);
   document.contentEditable = false;
}

Should work, also according to MDC you can set contentEditable on the element in question instead of the body and it should work.

Note: I do believe 3 parameters are mandatory in FireFox.

BGerrissen
Oh, my, I'm an idiot. Why didn't I think of such an obvious solution? Thank you.
Jordan
Don't punish yourself, I often overlook the simplest solutions as well when working with more complex code. I bet most people do ;)
BGerrissen
Be aware that contenteditable only made its way in to Firefox in version 3.0, so this won't work in Firefox 2 or earlier. You could just as well use `document.designMode = "on"` and `"off"` instead, which works in Firefox way back to pre-1.0 days.
Tim Down