Magento – Inserting Javascript codes in one CMS page of Magento 2.1

javascriptjquerymagento2

I am trying to add some javascript codes in one CMS page of a Magento 2.1 site. Here are what I did and the issues I am facing now:

1)at the "Layout Update XML" of the page, add the following jquery codes which will be used further:

The relevant Javacript codes come in the form like (in html file):

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    <script src="js/a.js" type="text/javascript"></script>
    <script src="js/b.js" type="text/javascript"></script>
    <script src="js/c.js" type="text/javascript"></script>
    <script src="js/d.js" type="text/javascript"></script> 
</head>

2) At the body of the content, add the following codes:

<p>some content</p>
<script type="text/javascript">
    require(['jquery'], function ($) {
     $(...).xxxxx
     //do stuff here
    });
</script>

The error shows now:
$(…).xxxxx is not a function


The codes without requireJS work well:

<script type="text/javascript">
    $(function () {
     $(...).xxxxx
     // do stuff here
    });
</script>

What am I missing here?

Thanks!

Best Answer

All javascript should use the require-js. In your case you should try something like:

    <script type="text/javascript">
        require(['jquery'],function($){   
           //$.doStuff
        });
    </script>

Im not sure if this will solve the external library. If not, then i guess you will have to add the external library to requirejs-config. There is plenty of information here:

Here