The updated codes in your question is not right. You need to change Theme_Module
to Magento_Theme
in the default.xml
.
Below is the complete code
In your myvendor/mytheme/Magento_Theme/layout/default.xml
, add content
<referenceContainer name="before.body.end">
<block class="Magento\Framework\View\Element\Template" template="Magento_Theme::js.phtml" name="module_js"/>
</referenceContainer>
Add the file js.phtml
inside directory myvendor/mytheme/Magento_Theme/templates
.
In the js.phtml
, add the js content like below
<script>
...
</script>
Refresh the site cache and you will see the effect.
Additional Tips
About the js.phtml content
The js content in Magento2 is another topic, you could find it it in official doc Calling and initializing JavaScript.
An official example is like below
<script>
require([
'jquery',
'accordion' // the alias for "mage/accordion"
], function ($) {
$(function () { // to ensure that code evaluates on page load
$('[data-role=example]') // we expect that page contains the <tag data-role="example">..</tag> markup
.accordion({ // now we can use "accordion" as jQuery plugin
header: '[data-role=header]',
content: '[data-role=content]',
trigger: '[data-role=trigger]',
ajaxUrlElement: "a"
});
});
});
</script>