app/design/frontend/Vendor/Theme/Magento_Theme/layout/default.xml
<referenceContainer name="after.body.start">
<block class="Magento\Framework\View\Element\Template" name="custom.js" template="Magento_Theme::custom_js.phtml"/>
</referenceContainer>
app/design/frontend/Vendor/Theme/requirejs-config.js
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
var config = {
map: {
'*': {
'respond': 'js/respond.min',
'customScript':'js/customscript'
}
},
paths: {
'bootstrap': 'js/bootstrap.min',
'owl_carousel': 'js/owl.carousel',
'custom':'js/custom',
},
shim: {
'bootstrap': {
'deps': ['jquery']
},
'owl_carousel': {
'deps': ['jquery']
},
'custom': {
'deps': ['jquery']
},
}
};
app/design/frontend/Vendor/Theme/Magento_Theme/web/js/customscript.js
define('jquery', function($) {
//Your code here
//alert('Here');
}(jQuery)
);
Our template will call our script: app/design/frontend/Vendor/Theme/Magento_Theme/templates/custom_js.phtml
<script>// <![CDATA[
require([
'jquery',
'customScript'
], function ($, script) {
//Your code here
//alert('Here');
});
// ]]>
</script>
Clear Magento Cache and run static content deploy: php bin/magento setup:static-content:deploy
Comments
Post a Comment