简体   繁体   中英

TinyMCE Unable to get property 'setContent' of undefined or null reference

I know this topic has been covered many times here but no matter which approach I take I either get a error such as "Unable to get property 'setContent' of undefined or null reference" or the line executes but nothing happens.

Here's what I know. tinymce initializes and is a valid object. The html variable has the propper HTML which it get from a parent window. jQuery is loaded and functional.

In addition to the code below I have also tried.

tinyMCE.activeEditor.setContent(html);
tinymce.editors[0].setContent(html);
$('textarea#XRMeditor').val(html);  * Before initialization 

I have tried all methods before and after tinymce intializes (just in case).

<!DOCTYPE html>
<html>
<head>

          <script src="sage_jquery.min.js" type="text/javascript"></script>
          <script src="tinymce_/tinymce.min.js" type="text/javascript"></script>

  <script>
      var html = window.parent.document.getElementById("descriptionIFrame").contentDocument.getElementsByTagName('body')[0].innerHTML;
      debugger;
      //$('textarea#XRMeditor').val(html);
      tinymce.init({
          selector: 'textarea#XRMeditor'
});
      tinymce.get('XRMeditor').setContent(html);

  </script>
</head>
<body>
  <textarea id="XRMeditor">Easy (and free!) You should check out our premium features.</textarea>
</body>
</html>

I suspect this issue is that you are trying to talk to TinyMCE before its initialized on the page.

Your get() call is in the head of your page and I would doubt that when the browser processes your script that TinyMCE has initialized. You can use an "init" handler to delay when this call happens:

tinyMCE.init({
  //your regular init parameters here...
  setup: function(editor) {
    editor.on('init', function() {
      //load your content here!
      tinymce.activeEditor.setContent(html);
      //or
      tinymce.editors[0].setContent(html);
    });
  }
});  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM