1

I have a quick question about tinyMCE. I have a textarea, with id="mainbuffer", and the function tinyMCE.get(id).setContent(data).

ONLY works when it is called onclick="function()" from a link and does not work when I load the page. In my case, it is: tinyMCE.get('mainbuffer').setContent( localStorage.getItem('mainbuffer') );

I've tried tinyMCE.execCommand(), but that doesn't work either.

I want the JavaScript function to initialize the textarea with data stored in localStorage, which I have checked, is working fine. Any suggestions?

Tiago Sippert
  • 1,324
  • 7
  • 24
  • 33
Matt
  • 43
  • 1
  • 7

1 Answers1

1

If you want to put script on page load, make sure you put your code in document.ready or after TinyMCE and your element has been loaded.

document.ready=function(){
   //debug document.getElementById('mainbuffer') return not undefined?
   tinyMCE.get('mainbuffer').setContent( localStorage.getItem('mainbuffer') );
}
James
  • 13,571
  • 6
  • 61
  • 83
  • Ahh thanks, I downloaded and installed firebug. It gives me the error: TypeError: tinyMCE.get("mainbuffer") is undefined. However, it should be defined. The script for the tinymce and the tinymce init is above the document.ready function. The textarea is in the body of the html. – Matt Aug 23 '12 at 04:03
  • The exact code: – Matt Aug 23 '12 at 04:22
  • 1
    Wow I think I found a better way. Instead of using any of that, I can just install the tinyautosave plugin that autosaves the content every set interval, and it handles localstorage. I'll try that. It took me a bit of rummaging through the message forum at tinymce.com/forum to find this plugin. – Matt Aug 23 '12 at 04:42