Jump to content

[1.5] addJS / CSS - controlling inject order


stratboy

Recommended Posts

Hi, addCSS and addJS, add the files BEFORE default js and css. That's not useful at all. In fact, it seems a bug. For example:

 

public function hookDisplayBackOfficeHeader($params){

 if(!(Tools::getValue('controller') == 'AdminModules' && Tools::getValue('configure') == 'homebanners')){
  return;
 }

 $this->context->controller->addCSS($this->_path.'back-office.css', 'all');
 $this->context->controller->addJS($this->_path.'back-office.js', 'all');

}

 

That add the files to my custom module. But the files are added before the default ones. So for example, before jquery was added. Before admin.css was added. And so on. ????

 

Anyone know if maybe there's some way to control the injecting order?

 

For now, my solution is to return html directly:

 

public function hookDisplayBackOfficeHeader($params){

 if(!(Tools::getValue('controller') == 'AdminModules' && Tools::getValue('configure') == 'homebanners')){
  return;
 }

 $html = '';
 $html .= '<link href="'.$this->_path.'back-office.css" rel="stylesheet" type="text/css" media="all" />';
 $html .= '<script type="text/javascript" src="'.$this->_path.'back-office.js" ></script>';

 return $html;
}

 

But maybe it's not the more elegant solution.

 

bye!

  • Like 1
Link to comment
Share on other sites

hmm just add js and css from admin footer hook

 

Mmm no, that's a workaround, but I was looking for some eventually 'hidden' feature instead, because I think it's quite strange there's no way to set the injecting order..

If I need a workaround, I prefer mine (the 'html' solution posted above). Also, injecting in footer maybe can solve js injection order but it's definitely not good for css.

Link to comment
Share on other sites

  • 2 years later...
public function renderForm()
{
   parent::__construct();
   //parent::setMedia();
   $this->addJS(_PS_MODULE_DIR_.'module/js/admin_product.js');
}

I call parent::__construct(); and then inject the needed CSS or JS, if not working try with uncommented parent::setMedia(); method. Working fine with presta 1.6.1.1

Link to comment
Share on other sites

  • 6 months later...

Hi,
 
I had the same Problem with PrestaShop 1.6.1.7, this is not working all times:
 

    public function hookdisplayBackOfficeHeader($params)
    {
        $this->context->controller->addCSS($this->_path . 'css/my.css', 'all');
        $this->context->controller->addJS($this->_path . 'js/my.js');
    }

 
Maybe with an 1&1-Auto-Installation it does not work because jQuery was includes later. This is my solution:
 

    public function hookdisplayBackOfficeHeader($params)
    {
        $this->context->controller->setMedia();
        $this->context->controller->addCSS($this->_path . 'css/my.css', 'all');
        $this->context->controller->addJS($this->_path . 'js/my.js');
    }

Many thanks to prestowicz for the inspiration!

Bye, Timo

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...