Inserting Javascript codes in one CMS page of Magento 2.1





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







2















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!










share|improve this question
















bumped to the homepage by Community 11 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • without requireJs its works, then what is the issue now, you can use without require js

    – Rakesh Jesadiya
    Sep 22 '16 at 7:05











  • sorry, without requirejs, means it works as a pure html + javascript page. means the javascript function works now. But it is not working on Magento page

    – greentealeaf
    Sep 22 '16 at 9:28











  • Please check this magento.stackexchange.com/questions/190821/…

    – Pravin
    Apr 30 '18 at 9:13


















2















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!










share|improve this question
















bumped to the homepage by Community 11 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • without requireJs its works, then what is the issue now, you can use without require js

    – Rakesh Jesadiya
    Sep 22 '16 at 7:05











  • sorry, without requirejs, means it works as a pure html + javascript page. means the javascript function works now. But it is not working on Magento page

    – greentealeaf
    Sep 22 '16 at 9:28











  • Please check this magento.stackexchange.com/questions/190821/…

    – Pravin
    Apr 30 '18 at 9:13














2












2








2








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!










share|improve this question
















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!







magento2 javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 22 '16 at 7:05







greentealeaf

















asked Sep 19 '16 at 15:06









greentealeafgreentealeaf

130113




130113





bumped to the homepage by Community 11 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 11 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • without requireJs its works, then what is the issue now, you can use without require js

    – Rakesh Jesadiya
    Sep 22 '16 at 7:05











  • sorry, without requirejs, means it works as a pure html + javascript page. means the javascript function works now. But it is not working on Magento page

    – greentealeaf
    Sep 22 '16 at 9:28











  • Please check this magento.stackexchange.com/questions/190821/…

    – Pravin
    Apr 30 '18 at 9:13



















  • without requireJs its works, then what is the issue now, you can use without require js

    – Rakesh Jesadiya
    Sep 22 '16 at 7:05











  • sorry, without requirejs, means it works as a pure html + javascript page. means the javascript function works now. But it is not working on Magento page

    – greentealeaf
    Sep 22 '16 at 9:28











  • Please check this magento.stackexchange.com/questions/190821/…

    – Pravin
    Apr 30 '18 at 9:13

















without requireJs its works, then what is the issue now, you can use without require js

– Rakesh Jesadiya
Sep 22 '16 at 7:05





without requireJs its works, then what is the issue now, you can use without require js

– Rakesh Jesadiya
Sep 22 '16 at 7:05













sorry, without requirejs, means it works as a pure html + javascript page. means the javascript function works now. But it is not working on Magento page

– greentealeaf
Sep 22 '16 at 9:28





sorry, without requirejs, means it works as a pure html + javascript page. means the javascript function works now. But it is not working on Magento page

– greentealeaf
Sep 22 '16 at 9:28













Please check this magento.stackexchange.com/questions/190821/…

– Pravin
Apr 30 '18 at 9:13





Please check this magento.stackexchange.com/questions/190821/…

– Pravin
Apr 30 '18 at 9:13










2 Answers
2






active

oldest

votes


















0














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






share|improve this answer
























  • thanks for the advice. Yes, the information you provided is very informative. Tried to add the JS codes with RequireJS, but now it shows "the xxx function is undefined", or "xxxx is not a function" error, on all browsers. Still trying to figure out the reason. Hopefully I can update it when I have it. Any helps will be appreciated, to benefit everybody. Thanks!

    – greentealeaf
    Sep 20 '16 at 7:13











  • Could you please update the question with your requirejs-config.js and the actual javascript in the body?

    – Langley
    Sep 20 '16 at 11:14



















0














Require isn't working for you as you're loading your javascript via XML, JS files should be loaded via x-magento-init or data-mage-init. For example:



<div data-mage-init='{"Magento_Theme/js/a": "{}"}'></div>


Also, I wouldn't recommend adding another version of jQuery, this could result in conflicts with the built-in version of jQuery. Also, why are you adding Javascript and code via the CMS?






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f137095%2finserting-javascript-codes-in-one-cms-page-of-magento-2-1%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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






    share|improve this answer
























    • thanks for the advice. Yes, the information you provided is very informative. Tried to add the JS codes with RequireJS, but now it shows "the xxx function is undefined", or "xxxx is not a function" error, on all browsers. Still trying to figure out the reason. Hopefully I can update it when I have it. Any helps will be appreciated, to benefit everybody. Thanks!

      – greentealeaf
      Sep 20 '16 at 7:13











    • Could you please update the question with your requirejs-config.js and the actual javascript in the body?

      – Langley
      Sep 20 '16 at 11:14
















    0














    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






    share|improve this answer
























    • thanks for the advice. Yes, the information you provided is very informative. Tried to add the JS codes with RequireJS, but now it shows "the xxx function is undefined", or "xxxx is not a function" error, on all browsers. Still trying to figure out the reason. Hopefully I can update it when I have it. Any helps will be appreciated, to benefit everybody. Thanks!

      – greentealeaf
      Sep 20 '16 at 7:13











    • Could you please update the question with your requirejs-config.js and the actual javascript in the body?

      – Langley
      Sep 20 '16 at 11:14














    0












    0








    0







    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






    share|improve this 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







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 19 '16 at 15:43









    LangleyLangley

    609320




    609320













    • thanks for the advice. Yes, the information you provided is very informative. Tried to add the JS codes with RequireJS, but now it shows "the xxx function is undefined", or "xxxx is not a function" error, on all browsers. Still trying to figure out the reason. Hopefully I can update it when I have it. Any helps will be appreciated, to benefit everybody. Thanks!

      – greentealeaf
      Sep 20 '16 at 7:13











    • Could you please update the question with your requirejs-config.js and the actual javascript in the body?

      – Langley
      Sep 20 '16 at 11:14



















    • thanks for the advice. Yes, the information you provided is very informative. Tried to add the JS codes with RequireJS, but now it shows "the xxx function is undefined", or "xxxx is not a function" error, on all browsers. Still trying to figure out the reason. Hopefully I can update it when I have it. Any helps will be appreciated, to benefit everybody. Thanks!

      – greentealeaf
      Sep 20 '16 at 7:13











    • Could you please update the question with your requirejs-config.js and the actual javascript in the body?

      – Langley
      Sep 20 '16 at 11:14

















    thanks for the advice. Yes, the information you provided is very informative. Tried to add the JS codes with RequireJS, but now it shows "the xxx function is undefined", or "xxxx is not a function" error, on all browsers. Still trying to figure out the reason. Hopefully I can update it when I have it. Any helps will be appreciated, to benefit everybody. Thanks!

    – greentealeaf
    Sep 20 '16 at 7:13





    thanks for the advice. Yes, the information you provided is very informative. Tried to add the JS codes with RequireJS, but now it shows "the xxx function is undefined", or "xxxx is not a function" error, on all browsers. Still trying to figure out the reason. Hopefully I can update it when I have it. Any helps will be appreciated, to benefit everybody. Thanks!

    – greentealeaf
    Sep 20 '16 at 7:13













    Could you please update the question with your requirejs-config.js and the actual javascript in the body?

    – Langley
    Sep 20 '16 at 11:14





    Could you please update the question with your requirejs-config.js and the actual javascript in the body?

    – Langley
    Sep 20 '16 at 11:14













    0














    Require isn't working for you as you're loading your javascript via XML, JS files should be loaded via x-magento-init or data-mage-init. For example:



    <div data-mage-init='{"Magento_Theme/js/a": "{}"}'></div>


    Also, I wouldn't recommend adding another version of jQuery, this could result in conflicts with the built-in version of jQuery. Also, why are you adding Javascript and code via the CMS?






    share|improve this answer




























      0














      Require isn't working for you as you're loading your javascript via XML, JS files should be loaded via x-magento-init or data-mage-init. For example:



      <div data-mage-init='{"Magento_Theme/js/a": "{}"}'></div>


      Also, I wouldn't recommend adding another version of jQuery, this could result in conflicts with the built-in version of jQuery. Also, why are you adding Javascript and code via the CMS?






      share|improve this answer


























        0












        0








        0







        Require isn't working for you as you're loading your javascript via XML, JS files should be loaded via x-magento-init or data-mage-init. For example:



        <div data-mage-init='{"Magento_Theme/js/a": "{}"}'></div>


        Also, I wouldn't recommend adding another version of jQuery, this could result in conflicts with the built-in version of jQuery. Also, why are you adding Javascript and code via the CMS?






        share|improve this answer













        Require isn't working for you as you're loading your javascript via XML, JS files should be loaded via x-magento-init or data-mage-init. For example:



        <div data-mage-init='{"Magento_Theme/js/a": "{}"}'></div>


        Also, I wouldn't recommend adding another version of jQuery, this could result in conflicts with the built-in version of jQuery. Also, why are you adding Javascript and code via the CMS?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 31 '18 at 12:25









        Ben CrookBen Crook

        9,2472477




        9,2472477






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f137095%2finserting-javascript-codes-in-one-cms-page-of-magento-2-1%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Alcázar de San Juan

            Griza ansero

            Heinkel He 51