addHeaderItem in Block

Permalink
I red a lot discussions about that topic in this forum, but no solution worked for me. The only thing i want to do is to add a js file to the header of my page.

I have added the following function to my block controller (its a block controller that overwrites the search block controller):
function on_start() {
 $html = Loader::helper('html');
 $this->addHeaderItem($html->javascript('searchCustom.js'));
}

The on_start function gets called but the file is not added to the header. If i do the following instead:
echo $html->javascript('searchCustom.js');

It works, but the code is added somewhere in the code (besides the block code). Adding to the header seems just to fail, whatever i do. I tried putting the js file in these folders: /js /themes/myTheme/js and besides the controller in /blocks/search/.

If i alter the block controller addHeaderItem function to echo the $file var it also echos the right code. its just not added to the header. I'm doing it in the on_start function, i tried on_before_render also... nothing.

Whats wrong?

malthoff
 
Krock replied on at Permalink Reply
instead of doing a function on_start have you just tried putting it in php tags?(put at very top of page)

<?php  
$html = Loader::helper('html');
$this->addHeaderItem($html->javascript('searchCustom.js'));
?>
Mnkras replied on at Permalink Reply
Mnkras
In blocks, just make a folder named js and/or css and the contents will be automatically added to the header
malthoff replied on at Permalink Reply
malthoff
That does only apply for blocks added via the C5 interface not for hard coded blocks.
jordanlev replied on at Permalink Reply
jordanlev
If you're hardcoding a block in your page type templates, then why don't you just put a link to the javascript file in your theme's <head>, like you would with any other javascript or css file?
malthoff replied on at Permalink Reply
malthoff
Finally i did but i wanted to learn why it is not working. Now i learned it is to late for the block in the overall process. Thanks for your answers.