4

I have a site that I put:

<body  onload="ajaxLoad()" > 

I have a Javascript function that then inserts data from my database into the text editor by using the setContent Javascript method of the textarea. It seems fine in Firefox and IE but in Chrome sometimes nothing shows up. There is no error, just a blank editor.

In the body section I have:

  <textarea id="elm1" name="elm1" rows="40" cols="60" style="width: 100%"> 
</textarea>

In the head section I have:

function ajaxLoad() {
        var ed = tinyMCE.get('elm1');
        ed.setProgressState(1); // Show progress
        window.setTimeout(function() {
            ed.setProgressState(0); // Hide progress
            ed.setContent('<p style="text-align: center;"><strong><br /><span   style="font-size: small;">General Manager&#39;s Corner</span></strong></p><p style="text-align: center;">August&nbsp;2009</p><p>It&rsquo;s been  15<sup>th</sup> and so have a Steak Night (Saturday, 15<sup>th</sup>) and a shore Dinner planned (Saturday, 22<sup>nd</sup>) this month. urday, September 5<sup>th</sup>. e a can&rsquo;t missed evening, shas extended it one additional week. The last clinic will be the week of August 11<sup>th</sup>. </p><p>&nbsp;Alt (Tuesday through Thursday) </p><p>&nbsp;I wouClub.</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;<strong></strong></p>');
        }, 1);
    }

I am not sure if its some of the formatting that Chrome is rejecting but it seems like if TinyMCE can parse it in one browser, it should be able to do it in any browser, so I am confused.

Any suggestions?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • Could you please specify the TinyMCE and Chrome versions you are using? This would ease reproducing the problem, especially if there would in fact be a correlation between Chromes DOM implementation and TinyMCEs requirements as suspected in my answer (http://stackoverflow.com/questions/1318291/tinymce-not-working-with-chrome-when-i-dynamically-setcontent/1397118#1397118). – Steffen Opel Sep 09 '09 at 02:55
  • tinymce 3.2.5 chrome beta channel – leora Sep 09 '09 at 13:33
  • have you solved this on windows? I mean not the onload issue.. just the insert in chrome. – Tony Nov 08 '11 at 11:25

7 Answers7

9

Background:

For various reasons onload() is not considered the proper approach for loading Javascript, see for example Launching Code on Document Ready, with the most important/noticeable one being that Javascript code isn't run until the page has finished downloading entirely, including images and the like, which might take an eternity therefore (e.g. if an external banner ad server is down etc.).

Instead it is recommended to load Javascript as soon as the DOM is ready, but unfortunately there is no cross browser compatible native solution for this, see Getting notified when the page DOM has loaded (but before window.onload); please note that my entire answer is based upon the most excellent Javascript library jQuery, which offers a cross browser solution for this problem, consequently I'd definitely favor the two higher voted answers over the accepted solution myself.

Likely cause:

Your issue seems to be caused by the opposite behavior though: Chrome facilitates the WebKit rendering engine, just like Safari, and for the latter onload() is discussed to behave differently, see section When does onload fire? in Is Safari faster?. Another description of this problem specific to Chrome can be found in window.onload not working correctly in Chrome, without an explanation though.

In conclusion I suspect onload() to fire before the DOM is actually loaded completely, at least concerning the requirements of TinyMCE, which is notoriously fragile regarding issues like this and simply ceases to load.

Possible solution:

Just facilitating attribute defer on the script tag as outlined in window.onload not working correctly in Chrome is again not cross browser compatible, hence I'd simply go with the widely deployed and proven approach of using the already mentioned jQuery cross browser solution for the onload() problem, which is good practice anyway and should in principle take care of your inverse issue as well.


Update:

There are indeed some bugs filed against WebKit which could back my conclusion (no matter whether this behavior actually constitutes a bug or is intentional), see:

Community
  • 1
  • 1
Steffen Opel
  • 63,899
  • 11
  • 192
  • 211
  • 1
    sure it's an approach to the onload issue... but the setContent behaviour still not work for chrome on windows. (latest versions of them both) using the method tiny.get("editor1").setContent('content'). FF7 reports the same problem on windows system (not in linux) annoying. – Tony Nov 08 '11 at 11:26
  • 1
    This probably doesn't apply directly, but I was working on an older site that worked fine in IE but wouldn't setContent in FF or Chrome. Turns out it was because of an outdated jQuery library in the project. I updated that and it works perfectly now - hope that helps someone out there! – deebs Jun 26 '15 at 14:56
3

I had a similar problem (editor not showing in chrome) and read in some forum, that if tinyMCE is unable to locate some files, it just stops loading. Try tracking down if everything is found using firebug's net tab (clear your cache first).

stefita
  • 1,785
  • 2
  • 20
  • 35
  • Thanks. (by mistake) I used this line in the in the setup: content_css : "http://www.tinymce.com/tryit/css/example.css", Apparently this file does no longer exist and TinyMCE stopped working i Chrome and Safari. I commented out this line and everything is working fine in webkit browsers. – eye-wonder May 03 '13 at 11:25
2

First of all; see to it that you have the latest version of TinyMCE.

I could not reproduce your problem given the information you provides. It seems just fine ( with the faked ajaxload ).

You could always try to go the "back entrance" in;

var myed = document.getElementById('elm1_ifr');
myed.contentDocument.getElementById('tinymce').innerHTML="<p style=\"text-align: center;\"><strong>hacking <span style=\"font-size: small;\">my</span> way in.</strong></p>";

Hope you good luck!

Frank
  • 2,640
  • 2
  • 21
  • 21
2

SOLUTION:

Ive been struggling with the TinyMCE toolbar not appearing in all kinds of secnarios....it would work in one user's IE browser, but not another. It would not work in Firefox or Chrome, etc.

Turns out if in the newest 3.3 version there are STILL bugs they have not fixed. This one occurs when in your webpage JavaScript code, where you instantiate the TinyMCE, if you added code for the plugin part where you load up two specific plugins, the toolbar fails to appear and you see HTML: plugins:

"safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,
inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,
fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,**imagemanager,filemanager**",

Remove all references to imagemanager and filemanager in your web page, then refresh your browser...

When you do, retest in Chrome and Firefox and IE and the TinyMCE suddenly appears! Wow....a miracle, huh?

Turns out their "main developer" hasn't fixed the issue but does explain it as such, if you want to try and still use those plugins and yet fix the problem. His quote is as follows:

"If the ImageManager/FileManager are old they might not use the new scriptloader api. And the new 3.3 version will load all dependent scripts async instead of sync. - Spocke - Main developer of TinyMCE"

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Stormy
  • 21
  • 1
  • 1
    For me deleting 'moxiemanager' did the trick. Didn't work in Chrome and Firefox before. Now it does. – KJS Aug 01 '13 at 21:22
1

There is an option oninit for the function tinyMCE.init(), put your method there:

tinyMCE.init({
    oninit: function(){
        tinyMCE.activeEditor.setContent('blah');
    }
});

It works this way.

The editor is not fully loaded on document load event, as it the editor loader will post-load a bunch of files.

Evgeny
  • 10,698
  • 9
  • 60
  • 70
1

There are a few ways to set content in a TinyMCE editor. If you want the content to be there when the page first loads, you can just put it between the tags.

Otherwise, you can do this through script in the following way:

window.onload = function() {

tinyMCE.getInstanceById("ProfileText").setContent('test');

};

I have wrapped the code in a window.onload block. If you have other functions that set content dynamically through this way after the TinyMCE editor has already loaded, then you do not need that.

Abu Arqam
  • 11
  • 1
0

same problem here, ie needs

var ed = tinyMCE.get('content');
ed.setContent('zzz');

other browsers

document.getElementById("content").innerHTML="zzz";

i am quite disapointed

will need to check browser in javascript to get it working properly, thats suxx