JS File Path: web->js->swap.js


    <referenceContainer name="before.body.end">
        <block class="Magento\Framework\View\Element\Text" name="swapjs">
                <argument name="text" xsi:type="string"><![CDATA[<script type="text/javascript" src="js/swap.js"></script>]]></argument>

File path not found 404 Error. Please help me to solve this issue.

2 Answers 2


Try Below way..

You can do it with layout

    <referenceContainer name="before.body.end">
        <block class="\Magento\Framework\View\Element\Template" template="You_Module::js.phtml" name="module_js"/>

and in js.phtml

<scrip src="example.com/jp2/test.js" />


<reference name="before_body_end">
        <block type="core/text" name="new-descriptive-name">
        <action method="setText">
            <text><![CDATA[<script type="text/javascript" src="http://www.mywebsite.com/myfolder/my-javascript.min.js"></script></text>
  • got the error Object DOMDocument should be created. Oct 18, 2019 at 11:53
  • @MasudShaikh i have updated my answer Oct 18, 2019 at 11:57
  • Ok, I will check. Oct 18, 2019 at 11:59

Add an external JS in the following way :

Try this,

Add your custom js in the following path.


then you need to add requirejs-config.js to the following path.


then add the following code in it

 var config = {
    paths: {            
            'your_js_name' : 'js/nameofjs', 
    shim: {
    'your_js_name': {
        deps: ['jquery']

and add the below code to load custom js, In your case it would be footer.phtml in Magento_Theme folder on your theme


require(['jquery','your_js_name'],function($) {
$(window).load(function () {
     /*alert('load from external jquery');*/

This would trigger the JS after footer is rendered. Hope this helps.

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.