0

I am using the TinyMCE to add content to CMS pages and static blocks. With TinyMCE editor disabled I add HTML and javascript code. For example

<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change HTML content.</p>
<button type="button" onclick='document.getElementById("demo").innerHTML = "Hello JavaScript!"'>Click Me!</button> 

Save the page and reopen it. The page opens with a TinyMCE editor showing the content. But when I switch to a simple editor to edit the content, the js code is stripped. This is what I see.

<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change HTML content.</p>
<p><button type="button">Click Me!</button></p>

I have this problem with Magento 2.3 & 2.4 What am I doing wrong?

Thanks

2 Answers 2

1

You are not doing anything wrong, it is just the behavior of the Wysiwyg text editor.

What I do recommend is not to add Javascript in the Wysiwyg editor at all (rather use code editor), but unfortunately once anyone switches from code editor to Wysiwyg editor and save; all your Javascript changes will be gone immediately.

Suggestion: Disable Wysiwyg editor completely.

However that prevents users that do not understand HTML to edit at all, if that is an issue, at least prevent Wysiwyg editor from being shown by default, in other words show code editor first.

Change default editor/Disable Completely

Go to the Backend settings -> Store -> Configuration -> General -> Content Management -> Wysiwyg settings -> Disable as default or Disabled Completely

3
  • Sometimes is much faster to use tinymce. Also not adding js to cms page is not an option. Many templates include js to cms even to homepage. Thank you for your reply
    – panosdotk
    Commented Apr 2, 2021 at 11:19
  • Welcome, but I have not said you cannot add js to the cms page. I said you can avoid it because as soon as you change the editor then js will get remove by it. and also this will not be going to remove the text editor only it will not activate by default. You have a button on top show/hide editor. You can switch it on your own will Commented Apr 2, 2021 at 11:32
  • I understand what you are telling me and thank you for your willingness to help me. I can not understand how an application like Magento manages the content of a page. In a magento installation, people with programming knowledge can work at the same time as well as people who simply add texts. And at this point there is a problem
    – panosdotk
    Commented Apr 2, 2021 at 11:52
1

enter image description here You can open js lib/web/tiny_mce_4/tinymce.min.js and search for extended_valid_elements and add y("script[src|async|defer|type|charset]"), in your tinymce.min.js .This will allow your javascript in TinyMCE. Warning: Allowing script elements () in TinyMCE exposes users to cross-site scripting (XSS) attacks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.