Tinymce (sometimes) undefined

I am using Tinymce (with jQuery) in a project that I am working on; we use a rich text editor for users to enter information; however, sometimes when loading pages Firefox and Chrome detect a "tinymce is not defined" error (sometimes in different lines of code), while other pages will load just fine. What is strange is that it works fine with IE.

Here is some code that I use:

view.find('textarea.rich-text').each(function () {        
   $(this).tinymce( /* ...rules... */);        
});  

And later

_variable.find("#summary").tinymce().setContent(content);

An error occurs on this line (sometimes). It seems to me that the problem is related to loading, although the tinyMCE plugin is initialized about 5,000 lines before this line.

Refresh . At the moment, I have managed to "solve" the problem with setTimeout, but this seems like a very ugly way to do it.

+5
2

:

  • , TinyMCE jQuery ready. .

  • . :

$('textarea.rich-text').tinymce({ script_url : '../js/tinymce/jscripts/tiny_mce/tiny_mce.js', theme : "advanced", ... });

  • find, id. :

$("#summary").tinymce().setContent(content);

  • , , , tinymce , . , script script_url. . , oninit.
+6

init TinyMCE, .

jQuery(document).ready(function($) {

    function myCustomSetContent( id, content ) {
        // Check if TinyMCE is defined or not.
        if( typeof tinymce != "undefined" ) {
            var editor = tinymce.get( id );
            // Check if TinyMCE is initialized properly or not.
            if( editor && editor instanceof tinymce.Editor ) {
                editor.setContent( text );
                editor.save( { no_events: true } );
            } else {
                // Fallback
                // If TinyMCE is not initialized then directly set the value in textarea.
                //TinyMCE will take up this value when it gets initialized.
                jQuery( '#'+id ).val( text );
            }
            return true;
        }
        return false;
    }

    function myCustomGetContent( id ) {
        // Check if TinyMCE is defined or not.
        if( typeof tinymce != "undefined" ) {
            var editor = tinymce.get( id );
            // Check if TinyMCE is initialized properly or not.
            if( editor && editor instanceof tinymce.Editor ) {
                return editor.getContent();
            } else {
                // Fallback
                // If TinyMCE is not initialized then directly set the value in textarea.
                // TinyMCE will take up this value when it gets initialized.
                return jQuery( '#'+id ).val();
            }
        }
        return '';
    }

    $(".class-to-update-content").on("click", function(e) {
        myCustomSetContent( "tinymce-editor-id", "New Content in Editor" );
    });

    $(".class-to-get-content").on("click", function(e) {
        $("div.class-to-display-content").html( myCustomGetContent( "tinymce-editor-id" ) );
    });
});

: http://blog.incognitech.in/tinymce-undefined-issue/

EDIT:

0

All Articles