You have to set jQuery dependency for your custom script. You have
occurance of problem because of dependency issue.
- For Module/Extension Level
You can set at module/extension level dependency like below,
app/code/Packagename/Modulename/view/frontend/requirejs-config.js
var config = {
paths: {
'slider': "Packagename_Modulename/js/slider"
},
shim: {
'slider': {
deps: ['jquery']
}
}
};
Add slider.js inside path, app/code/Packagename/Modulename/view/frontend/web/js/slider.js
You can use in any template file like this,
<script>
require(["jquery","slider"],function($)(){
$(document).ready(function() {
$(element).slider({
//option
});
});
});
</script>
- Theme Level
If you want to apply require js changes at theme level,
Go to theme module,
app/design/frontend/{Packagename}/{themename}/Packagename_Modulename/requirejs-config.js
Set your theme required js file in requirejs-config.js
var config = {
paths: {
'slider': "Packagename_Modulename/js/slider",
'customjsfile': "Packagename_Modulename/js/customjsfile",
},
shim: {
'slider': {
deps: ['jquery']
},
'customjsfile':{
deps: ['jquery']
}
}
};
Add your js file inside web folder of theme,
app/design/frontend/{Packagename}/{themename}/Packagename_Modulename/web/js/slider.js
app/design/frontend/{Packagename}/{themename}/Packagename_Modulename/web/js/customjsfile.js
In your template file,
<script>
require(["jquery","slider","customjsfile"],function($)(){
$(document).ready(function() {
$(element).slider({
//option
});
});
});
</script>
For your case,
Keep below config in requirejs-config.js file,
var config = {
paths: {
'footerContact': 'Module_Contact/js/contact'
},
shim: {
'footerContact': {
deps: ['jquery']
}
}
};
Inside phtml file,
<script>
require(["jquery","footerContact"],function($)(){
jquery('.element').on('click', function(event) {
console.log('logging');
});
});
</script>
I had the same problem and just solved it by re-attaching Popper to the global scope and nesting the bootstrap dependency in my javascript.
app.js
require(['jquery', 'popper'], function($, Popper) {
window.Popper = Popper; // re-attach to global scope
require(['bootstrap'], function() {
$(function() {
// This function is needed (even if empty) to force RequireJS to load Twitter Bootstrap and its Data API.
// You can make calls to bootstrap functions here.
});
});
});
I'm requiring a little different so I'll attach these just in case.
requirejs-config.js
var config = {
paths: {
'popper': 'js/popper',
'bootstrap': 'js/bootstrap'
},
shim: {
'popper': {
'deps': ['jquery'],
'exports': 'Popper'
},
'bootstrap': {
'deps': ['jquery', 'popper']
}
}
};
default_head_blocks.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="css/app.css" />
<script src="js/app.js" type="text/javascript" />
</head>
</page>
Best Answer
The best way to add
JS
is with requirejs.To add a js in theme via
Requirejs
:Supposing that your js file is:
myfile.js
app/design/frontend/{Vendor}/{theme}/requirejs-config.js
app/design/frontend/{Vendor}/{theme}/web/js/myfile.js
app/design/frontend/{Vendor}/{theme}/Magento_Theme/templates/{yourfile}.phtml
Info: don't forget to :
clean the cache
clean
var/view_preprocessed
contentclean
pub/static
contentdeploy the static content =
php bin/magento setup:static-content:deploy -f