Magento – Magento 2 add script before closing body tag

javascriptmagento2

What is the proper way to add a script before the closing body tag in magento 2?

I want it to be available to all pages.

UPDATE:

I've got in myvendor/mytheme/Magento_Theme/layout/default.xml with:

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

And the js.phtml inside /Magento_Theme/templates

Should that be right?

Best Answer

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"/>
    </referenceContainer>

and in js.phtml

<script src="//cdn.javascrip.com/test.js" />