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>
Finally this is a solutions that works for me.
https://themeforest.net/item/porto-ultimate-responsive-magento-theme/9725864/comments?page=293#comment_16606311
"Kroellie
Finally figured out the solution for javascript messages:
Uncaught TypeError: $(...).swMegamenu is not a function
Uncaught TypeError: $(...).stellar is not a function
Uncaught TypeError: $(...).owlCarousel is not a function
Uncaught TypeError: $.widget is not a function
If you suffer the same, do this:
create a requirejs-config.js in the root of the child theme
app/design/frontend/Smartwave/porto_child and put in the following:
var config = {
shim: {
jquery: {
exports: '$'
},
'Smartwave_Megamenu/js/sw_megamenu':
{
deps: ['jquery']
},
'owl.carousel/owl.carousel.min':
{
deps: ['jquery']
},
'js/jquery.stellar.min':
{
deps: ['jquery']
},
'js/jquery.parallax.min':
{
deps: ['jquery']
}
}
};
And change the entries:
<script src="jquery.js" />
<script src="bootstrap/js/bootstrap.min.js" />
<script src="fancybox/js/jquery.fancybox.js" />
To:
<remove src="jquery.js" />
<remove src="bootstrap/js/bootstrap.min.js" />
<remove src="fancybox/js/jquery.fancybox.js" />
Within file:
app/design/frontend/Smartwave/porto_child/Magento_Theme/layout/default_head_blocks.xml
Tada! No more need for merging javascripts, no more javascript errors!
I have no need for the fancybox, turned it off, but if you do require it, I guess it should be included in the requirejs as well. "
Best Answer
Firstly, you need to make sure, that your module has
Magento_ConfigurableProduct
in sequence inmodule.xml
:Make sure to regenerate the component list in
config.php
, otherwise the sequence will be ignored (http://devdocs.magento.com/guides/v2.2/extension-dev-guide/build/module-load-order.html)Then add
requirejs-config.js
file inview/frontend
directory with code:Finally add
configurable.js
file inview/frontend/web/js
directory with:You can't modify a single line, but you can modify a single function inside.