大きく分けて2通りのやり方があるようですが、簡単なやり方でやります。
まずはMagento2 themeの作成でテーマを作成しておきます。
以下のパスに default_head_blocks.xml を設置します。
app/design/frontend/Yassujp/blank/Magento_Theme/layout/
default_head_blocks.xml
<?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> <link src="js/custom.js"/> </head> </page>
以下のパスに custom.js を設置します。
app/design/frontend/Yassujp/blank/web/js/
以下のコマンドで静的ファイルをデプロイします。
php bin/magento setup:static-content:deploy
デプロイ先は以下になります。
pub/static/frontend/Yassujp/blank/en_US/js/custom.js
更新されない場合は削除してしまって再度コマンドを叩きました。
# rm -fr pub/static/frontend/Yassujp/
SYSTEM - Cache Management からキャッシュをクリアします。
これでJSが実行されればOKです。
Magento2でjQueryを使用するも参考にして下さい。