Magento Version: 2.0.0

There have some javascript files should be loaded into the template. How to load them such as importing the css file in tag?

    <css src="My_Module::css/style.css"/>
    <script src="My_Module::js/script.js" />

Here is the example. However, you need to take into account, that this script.js will be loaded synchronously, unlike all core scripts, like jquery.js (that are loaded via requirejs) so you can't use for example $ in your script.js. If you do want to use all core scripts features, register your script using requirejs-config.js. Good explanation is here How to load custom module js file in magento 2?

  • your code is working now. but i want to use require.js to load the custom js file. so, could you check my another question where is wrong in my configuration? magento.stackexchange.com/questions/107045/… Thanks.
    – Mike Zhang
    Mar 20 '16 at 1:23
  • @mike this reference gives 404 for me Mar 20 '16 at 16:23
  • thanks. i have found the solution because i made a mistake on my code. So, i deleted my question.
    – Mike Zhang
    Mar 20 '16 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.