How to set the initial text in a TinyMCE textarea?

て烟熏妆下的殇ゞ 提交于 2019-11-30 12:36:27
gradbot

If you don't call tinyMCE's JavaScript function, you may have to deal with browser compatibility issues.

If you only have one tinymce box on the page, you can just do this:

tinyMCE.activeEditor.setContent('<span>some</span>');

You can also set a format such as BBCode. Check out the setContent documentation.

I would have your PHP code echo out the HTML into a JavaScript function and have it called with onload:

function loadTinyMCE($html) {
    echo "
        <script type="text/javascript">function loadDefaultTinyMCEContent(){
             tinyMCE.activeEditor.setContent('$html');
        }</script>
    ";
}

then

<body onload="loadDefaultTinyMCEContent()">

If you don't want to use the page onload event, tinymce has an init option called oninit that functions similarly.

Alternatively, setupcontent_callback gives you direct access to the iframe.

Given that the presence of <p> tags causes the transformation, I suspect that your HTML ends up looking like:

...
<textarea id="editing_field"><p>This text is supposed to appear in the rich textbox</p></textarea>
...

Unfortunately, HTML elements aren't technically allowed inside <textarea> tags, so something may be considering the <p> tags as the start of a new block element, implicitly closing your textarea. To fix this, you can encode the angle brackets:

...
<textarea id="editing_field">&lt;p&gt;This text is supposed to appear in the rich textbox&lt;/p&gt;</textarea>
...

Using PHP, this can be done by sending your value through htmlspecialchars() before echoing it to the page.

That second example <textarea ... /> is empty tag notation. If you want the text to be inside the tage you need to do it like the first way <teatarea ....>foo</textarea>

What is outputting the textarea tags? Can you debug that to see if it is doing the empty tag notation?

Eric

tinyMCE.activeEditor.setContent('some') works for me.Cheers

We're using TinyMCE here lately as well. I'm particularly puzzled about the change the the style attribute of the textarea to hide the element. Very odd.

I'm curious what your configuration file looks like (some documentation on configuration files at: django)


UPDATE

wow... you're using a LOT of tinyMCE plugins. I'll try to research further when I get some time and see if I can't figure out some likely suspects.

It appears that I have solved the problem, unless there are any edge cases which ruins the solution. I use the following PHP code on page content before I save it to the database:

$content = str_replace(chr(10), "", $content);
$content = str_replace(chr(13), "", $content);
$content = str_ireplace('<','&#x200B;<',$content);

What it does is it removes any newlines and then prepend a zero-width invisible character before any beginning tag. This text is then later inserted between the textarea tags before TinyMCE does its magic. I don't know why but this does not trigger the problem and the appended characters are not shown in the final html or in the html view of TinyMCE, so the only problems I can see with this solution is the performance hit. A detail is that it appears only the start tags need to be prepended in this way, but I haven't taken this into consideration here, for simplicity.

I've come to the same problem while using React, perhaps it's related to this. The initial value passed to the component is the original value. Once received, the component will take over and handle the content, regardless of whether a new value is passed to it. So if you are passing a string, then it's a constant and it exists at initial render of your component. But if you are passing a variable, it's possible that the initial value of the variable is different to the value you want to display (like it's probably null or undefined until you receive the value to display). In my case I receive an object with the page contents, one of which is the html to display as initial content. But the render method was first being fired with an empty object, and then with the actual object with data.

set in tinyMCE.init -

init_instance_callback: function (editor) {
                AFunction();
            }

function AFunction(){
//your code here
tinyMCE.get('youtinytextareaname').setContent("your text here");
}

To change an specific tinymce field, you can do this:

tinyMCE.get('editing_field').setContent('your default text');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!