5

I am using tinymce in my application for a rich text box experience, I am getting this weird problem while using it in Mozilla. It works perfectly while using it first time, but if i load same tinymce instance second time it is giving below error in console screen.

NS_ERROR_UNEXPECTED

Please suggest the solution if any , Below is the code I am using:

 <script type="javascript">
    tinymce.remove();
    jQuery('#tinymce').tinymce({
        height: 250,
        width: 750,
        entity_encoding: "raw",
        encoding: "xml",
        force_br_newlines: false,
        force_p_newlines: false,
        forced_root_block: 'div',
        statusbar: false,
        theme: 'modern',
        menubar: false,
        plugins: "mention " + plugins,
        toolbar1: toolbar1,
        toolbar2: toolbar2
});
</script>
tuxayo
  • 1,150
  • 1
  • 13
  • 20

4 Answers4

6

To get rid of this problem simply wrap tinymce.remove() in try catch.

    try {
        tinymce.remove("#id");
    } catch (e) {}

The cause of this problem is that while the editor does not exist in DOM, tinyMCE script thinks it's still out there. You can check it by placing a breakpoint on tinymce.remove() and running tinymce.editors.length - it will return a positive value even if the editor is not there. For some unknown reason the problem appears only in Firefox.

Pramus
  • 390
  • 3
  • 20
3
while (tinymce.editors.length > 0) {
    try {
        tinymce.remove(tinymce.editors[0]);
    } catch (e) {
        // alert(e);
    }
}

Try the code above. It should work fine.

brennanyoung
  • 6,243
  • 3
  • 26
  • 44
MTK
  • 51
  • 5
0

This error is annoying. It has to do with a racing condition. Try to put your remove command in a setTimeout-Block before you reinit the editor and it should work.

setTimeout(function(){
    var ed = tinymce.get('your_editor_id');
    if (ed) ed.remove();
}, 1);

Update: Then try something different:

var ed = tinymce.get('your_editor_id');
if (ed) ed.remove();

setTimeout(function(){
  jQuery('#tinymce').tinymce({
    height: 250,
    width: 750,
    entity_encoding: "raw",
    encoding: "xml",
    force_br_newlines: false,
    force_p_newlines: false,
    forced_root_block: 'div',
    statusbar: false,
    theme: 'modern',
    menubar: false,
    plugins: "mention " + plugins,
    toolbar1: toolbar1,
    toolbar2: toolbar2
   });
}, 1);
Thariama
  • 50,002
  • 13
  • 138
  • 166
  • Hi Thariama, Thank you for your input, I have tried your solution , but if i put remove command in timeout then it stopped working on the first try as remove command is being executed after init command, hope you are getting me!! – kartik ghodasara Oct 23 '15 at 10:08
  • then try something different, make sure to use the correct editor id – Thariama Oct 23 '15 at 11:23
  • Hi Thariama, Yes the editor id was correct, I tried your updated solution also, but this issue is arising while executing remove command, I mean at the line where 'ed.remove()' is being executed while opening the tinymce second time. – kartik ghodasara Oct 26 '15 at 05:58
  • what happens if you raise the timeout duration? – Thariama Oct 26 '15 at 08:23
  • It doesn't matter , as the exception is being thrown where remove command is being executed. Hope you are getting me!! – kartik ghodasara Oct 27 '15 at 05:03
  • i think the problem is that your editor ionstance is not shut down correctly no matterwhat. have a look here to see how this can be done: http://stackoverflow.com/questions/17759111/tinymce-4-remove-or-destroy – Thariama Oct 27 '15 at 08:15
  • tried this one already , unfortunately it is also of no help in case of mozilla – kartik ghodasara Nov 03 '15 at 11:07
  • hmm, i am out of options then, sorry – Thariama Nov 03 '15 at 11:36
0

In my case all suggestions doesn't work.

I used this code to resolve my issue with Firefox and NS_ERROR_UNEXPECTED:

if( typeof window.tinymce != 'undefined' && $(window.tinymce.editors).length > 0 ){
  $(window.tinymce.editors).each(function(idx) {
    try {
    tinymce.remove(idx);
    } catch (e) {}
  });
}

Hope this will help to someone.

speksy
  • 700
  • 8
  • 13