0

I am trying to add some JS to the CMS homepage.
I tried to add an external js file as well as adding the code directly. See code below.
When I save the CMS page and test everything the js works fine.

However,when I reload the CMS in the Backend again to add more content the js script apparently isn't loaded from the db therefore the whole <script> section gets removed from the cms page and when I save it again is removed from the page.

How do I solve this?

Code:

<script type="text/javascript">
require([
    'jquery',
    'slick'
], function ($) {
    jQuery(document).ready(function () {
        jQuery('.slider').slick();
    });
});
</script>

and with external file:

<script type="text/x-magento-init">
{
  "*": {
    "Magento_Theme/js/slider": {}
  }
}
</script>
0

In the CMS Page, you can use try the below way.

<script type="text/javascript">
    <![CDATA[//
        require([
            'jquery',
            'slick'
        ], function ($) {
            $(document).ready(function () {
                $('.slider').slick();
            });
        });
    //]]>
</script>

Hope this will work for you.

1
  • unfortunately it doesn't The code still gets removed completely – Chaz Apr 9 '20 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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