简体   繁体   中英

Adding a script tag to every product page in magento 2 using module

I'm new to magento.

I'm created a module-2 module from below mentioned tutorial.

http://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/

With that module I'm injecting script into the page. But with this I'm limited to execute only on my module page ie,

http://localhost/magento2/helloworld

I have a sample store installed, but what should I do if I want my module to be executed on any product page.

Please help me understand. Pointing to an article would be greatly appreciated.

Thank you.

Call your css and js file inside of tag in the layout xml. Example:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <link src="Namespace_YourModule::script.js"/>
        <css src="Namespace_YourModule::style.css"/>
    </head>
    <body>
    </body>
</page>

I found the article that solves this specific problem.

https://mage2.pro/t/topic/34

In this he has added script files by default rather than by route.

edit: In another article he has given information on conditional loading of script and css files.

http://www.webspeaks.in/2016/03/how-to-add-css-and-js-in-magento-2-custom-module.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM