I have created a Javascript widget which does some function based on product add/remove cart action.

I would like to include the JS only on product pages viz: Category and Product detail page.I know a way, where I can create a phtml template and call the js via script by injecting the template on specific pages via XML.

However, I would like to know if there is a way to inject JS widget on particular page type without creating a phtml template for it?

1 Answer 1


You could add the script instruction to your category and product layout XML files.

  • catalog_product_view.xml
  • catalog_category_view.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <link src="Your_Module::js/your-file.js"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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