Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everybody,

I am trying to make an simple Editor but some Javascript cods not working with Firefox like:

JavaScript
document.getElementById(rteName).contentWindow.document.execCommand(‘insertHTML’, false, html);


insertHTML is working with some tags in Firefox like <img> tag but not work with others like:
<a> tag, <span> tag, ... etc.

any advice please
Posted
Updated 4-Dec-11 14:48pm
v5
Comments
DaveAuld 4-Dec-11 5:25am    
Do you mean innerHTML?
MrLonely_2 4-Dec-11 6:24am    
Noooo
[no name] 4-Dec-11 20:48pm    
EDIT: added code block

1 solution

This applies to Rich Text editing in web browsers.

Usually almost every editor I saw uses iframes as editing areas, something like this:

- the iframe:
HTML
<iframe id="editor" src="editable.html" style="width:500px;height:300px;border:solid 1px red;"></iframe>


- and the content for "editable.html":
HTML
<html>
<head>
    <title>Editable content example</title>
    <meta charset="utf-8" />
</head>
<body contenteditable="true">
    Some text in the editor.
</body>
</html>


- and the script you need for your problem should be something similar to this (tested in latest version of firefox, but should work from FF3 up):
JavaScript
<script type="text/javascript">
function InsertHtml() {
    var html = 'text for testing';

    var editor = document.getElementById("editor");
    var editorDoc;
    if (editor.contentDocument)
        editorDoc = editor.contentDocument;
    else
        editorDoc = editor.contentWindow.document;
    editorDoc.execCommand('insertHTML', false, html);
}
</script>


If you will use another HTML element as wrapper for your editor, like a DIV:
HTML
<div id="rteName" contentEditable="true" style="width:500px;height:300px;border:solid 1px red;">

and use the following script or your script, you will get "contentWindow" is undefined.
I don't really know the reason for this, but this is how it works.

And another thing, in Chrome it only works if you are inside the editor (the editor area is active).

Hope it helps,
Valy.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900