i have created my custom module in that a particular .phtml file i want to apply my custom JS
please suggest as soon as possible
Hi @asd_asd46
If you want to add js with phtml file then you can consider below code and add with phtml file
<script>
require(['jquery', 'myscript'], function($, myscript) {
cosole.log("testing");
});
</script>Or you can refer below link :
https://magento.stackexchange.com/questions/301627/magento-2-how-to-include-custom-js-file-in-phtml-...
If issue resolve, please click on 'Kudos' & Accept as Solution!
Hello @asd_asd46 ,
please consider the following solution to add js in .phtml file,
Magento's frontend app engine searches for all script tags with
type =text/x-magento-init
and uses the contents of it to call out certain jQuery widgets on certain DOM elements.
<script type="text/x-magento-init">
{"*": {"Vendor_Module/main": {}}}
</script>
Vendor_Module/main refer to the JavaScript file in Vendor/Module/view/<area>/web/main.js folder.
An example of such a file:
require([
'jquery', 'SimpleMDE', 'HighlightJs'
], function($, SimpleMDE, HighlightJs) {$(function() {
// Your custom code here...
});});You can refer below link for more,
https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/javascript/js_init.html
Thank You
Problem Solved? Click on 'Kudos' & Accept as Solution!
Thank you for valuable suggestion.Give sample code is working in my .phtml file
But i have 2 files
1) Jacker_scroller.js and 2) jquery.pagepepling.js
i want to include these both files in .phtml please suggest on this
1) Jacker_scroller.js
Thank you for valuable suggestion.Give sample code is working in my .phtml file
But i have 2 files
1) Jacker_scroller.js and 2) jquery.pagepepling.js
i want to include these both files in .phtml please suggest on this
Hello @asd_asd46 ,
Standard Syntax for type="text/x-magento-init" is
<script type="text/x-magento-init">
{
// components initialized on the element defined by selector
"<element_selector>": {
"<js_component1>": ...,
"<js_component2>": ...
},
// components initialized without binding to an element
"*": {
"<js_component3>": ...
}
}
</script>please refer below link for the detailed info
https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/javascript/js_init.html
Problem solved? accept the solution and click kudos.