Jump to content

JS not added


Recommended Posts

Hi, I am using PS 1.7

I just want to add an js file, but, i am struggling to add it for hours.

Read alot of answers, no success.

I register the header and hook it:

public function install()
{
    if (Shop::isFeatureActive()) {
        Shop::setContext(Shop::CONTEXT_ALL);
    }

    if (!parent::install() ||
        !$this->registerHook('leftColumn') ||
        !$this->registerHook('header') ||
        !$this->registerHook('footer') ||
        !Configuration::updateValue('MYMODULE_NAME', 'my friend')
    ) {
        return false;
    }

    return true;
}

public function hookHeader()
{
    //$this->context->controller->registerJavascript('mymodule', 'modules/mymodule/js/matomo.js', ['position' => 'bottom', 'priority' => 150]);
    $this->context->controller->addJS($this->_path . '/js/matomo.js');//path of js
}

But my matomo.js file is not showing up :( what do i do wrong ?

Thanks

Link to comment
Share on other sites

Hi !

To add JS there is 2 possibilities.

1-
First one is to add it in your module front controller.

	public function setMedia()
	{

		$module = 'YOURMODULENAME';
		parent::setMedia();

		$this->addJS(_MODULE_DIR_.$module.'/views/js/front.js');
		$this->addCSS(_MODULE_DIR_.$module.'/views/css/front.css');

	}

 

2-

Second one is to add it like you do on hookHeader() in your module.php file.
But this JS will be trigged on all pages.

Be sure that your path is correct.
Is it in 'views' folder ? > then add it.

Try to remove the first '/' of your path (right before 'js') :
$this->context->controller->addJS($this->_path . 'js/matomo.js');//path of js

Mine looks like this :
$this->context->controller->addJS($this->_path.'views/js/back.js');

I hope it will help

Link to comment
Share on other sites

hi.. 

This is correct way in prestashop 1.7 please check the any cache is enable for your store ?

public function hookHeader()
{
    $this->context->controller->registerJavascript('mymodule', 'modules/mymodule/js/matomo.js', ['position' => 'bottom', 'priority' => 150]);
    //$this->context->controller->addJS($this->_path . '/js/matomo.js');//path of js
}

 

Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...