cancel
Showing results for 
Search instead for 
Did you mean: 

Adding script in all pages using custom module in magento 2

SOLVED

Adding script in all pages using custom module in magento 2

Can Someone please tell me how to add custom javascript and link in header using custom module in all CMS pages.

 

I don't wanna create any specific theme.

 

My javascript should be available for all themes.

 

Tried to create a block but that is also not working as block is also specified to a particular theme.

 

2 REPLIES

Re: Adding script in all pages using custom module in magento 2

Hi @Wigzo_Magento,

 

Assuming your script is located in <module_root>/view/frontend/web/script.js , the simplest way to add a script to all pages and all themes you can create a module containing <module_root>/view/frontend/layout/default.xml file with the following contents:

 

<?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="YourNamespace_YourModule::script.js"/>
    </head>
</page>

 

Re: Adding script in all pages using custom module in magento 2

Thank You. Your feedback was very useful. But when I apply this in custom theme I am not able to open the js file it is showing me an error saying

"Autoload error createApplication('Magento\Framework\App\Http'); * $bootstrap->run($app); * -------------------------------------------- * * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ try { require __DIR__ . '/app/bootstrap.php'; } catch (\Exception $e) { echo <<"

 

Thanks once again