I used custom theme in magento 2. I want to add custom js file for navigation in magento 2 but js file not work.
I add js link in default_head_block.xml but a js doesn't work.
<link src="js/menu.js"/>
-
1Don't do this, man. Use Magento devdocs devdocs.magento.com/guides/v2.2/javascript-dev-guide/javascript/…. if you include scripts in this way - you will have a lot of problems.Volodymyr Lisniak– Volodymyr Lisniak2018年12月06日 09:38:37 +00:00Commented Dec 6, 2018 at 9:38
-
@Gagan, could you please tell me how you succeeded in accomplishing this?CodeForGood– CodeForGood2020年05月10日 14:58:09 +00:00Commented May 10, 2020 at 14:58
3 Answers 3
create the file /app/design/frontend/Vendor/customtheme/Magento_Theme/layout/default_head_blocks.xml
assume you are trying to add bootstrap.js
File : default_head_blocks.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<!--Include js From CDN-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" src_type="url" />
<!--Include js From Custom module app/code/Company/Module/view/frontend/web/js-->
<script src="Company_Module::js/bootstrap.min.js"/>
</head>
</page>
Please remove static files and flush magento cache
-
I want to add this script <script src="code.jquery.com/jquery-3.3.1.min.js"></script>Gagan– Gagan2018年12月06日 09:55:59 +00:00Commented Dec 6, 2018 at 9:55
-
please update default_head_blocks.xml with <script src="code.jquery.com/jquery-3.3.1.min.js" src_type="url" />Pritam Biswas– Pritam Biswas2018年12月06日 10:03:31 +00:00Commented Dec 6, 2018 at 10:03
Goto your default.xml to the below location:
app/design/frontend/Vendor/theme/Magento_Theme/layout/default.xml
And add the below code:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script src="Magento_Theme::js/custom.js"/>
</head>
</page>
Now place your custom.js under below location:
app/design/frontend/Vendor/theme/Magento_Theme/web/js/custom.js
Now you can add your to the js file as you want.
-
<?xml version="1.0"?> <page xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <script src="Magento_Theme::js/menu.js"/> </head> </page> Add js file but js file not loadedGagan– Gagan2018年12月06日 09:50:23 +00:00Commented Dec 6, 2018 at 9:50
-
Did you flush the cache? Also you need to run static-content:deploy as well.Sukumar Gorai– Sukumar Gorai2018年12月06日 10:09:31 +00:00Commented Dec 6, 2018 at 10:09
-
@Sukumar Gorai, could you please have a look at my post regarding the similar task with slightly varying requirement at magento.stackexchange.com/questions/312387/…CodeForGood– CodeForGood2020年05月10日 15:41:49 +00:00Commented May 10, 2020 at 15:41
The best way to add a JS in Magento (theme or module) 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
var config = {
map: {
'*': {
myscript: 'js/myfile'
}
}
};
app/design/frontend/{Vendor}/{theme}/web/js/myfile.js
define(['jquery'], function($){
"use strict";
return function myscript()
{
alert('hello myscript');
//put all your myfile js code here
}
});
app/design/frontend/{Vendor}/{theme}/Magento_Theme/templates/{yourfile}.phtml
<script type="text/javascript">
require(['jquery', 'myscript'], function(,ドル myscript) {
myscript();
});
</script>
Info: don't forget to :
clean the cache
clean
var/view_preprocessedcontentclean
pub/staticcontentdeploy the static content =
php bin/magento setup:static-content:deploy -f
-
what if I need to place the javascript url in the header of my Magento site?CodeForGood– CodeForGood2020年05月10日 13:34:05 +00:00Commented May 10, 2020 at 13:34
-
could you please tell me if I can also use your method if I have placed the custom js file in a directory media/pmm-custom?CodeForGood– CodeForGood2020年05月10日 15:00:59 +00:00Commented May 10, 2020 at 15:00
-
I don't want to keep my custom file here in app/design/frontend/{Vendor}/{theme}/web/js/.CodeForGood– CodeForGood2020年05月10日 15:15:50 +00:00Commented May 10, 2020 at 15:15