adding javascript to header from a block
Permalink 1 user found helpful
Hi,
New to concrete5 and need some help. I am trying to add js from the block controller and place it in the header.
$this->addFooterItem(('<script>//my js goes here</script>');
This works but addHeaderItem does not(due to the rendering order?), is there any way I can use on_before_render() or other events to get this code in the header?
New to concrete5 and need some help. I am trying to add js from the block controller and place it in the header.
$this->addFooterItem(('<script>//my js goes here</script>');
This works but addHeaderItem does not(due to the rendering order?), is there any way I can use on_before_render() or other events to get this code in the header?
The appropriate event in a block controller is on_page_view. Elsewhere (such as in a single page controller) use on_start.
Thanks John, I looked at on_page_start and would be ideal. The problem is each block has some dynamic code, so the addHeaderItem has to be called every time the a block is created.
At the moment, this only adds one line of JS into my header when there are 3 blocks on the page so there should be 3 lines of javascript.
At the moment, this only adds one line of JS into my header when there are 3 blocks on the page so there should be 3 lines of javascript.
The block controller (with on_page_view event) is run for each instance of each block before the page is rendered. You can also put actual javascript code (rather than file links) within add header item.
So as long as you can move enough of your controller's view() code into functions that can be called from on_page_view, I would have assumed that should be enough to solve your problem.
(Edited to correct typo)
So as long as you can move enough of your controller's view() code into functions that can be called from on_page_view, I would have assumed that should be enough to solve your problem.
(Edited to correct typo)
on_page_view is called one time
on_page_load is called 0 times I cannot see it here
http://www.concrete5.org/documentation/developers/system/events/...
is it a new function?
on_page_load is called 0 times I cannot see it here
http://www.concrete5.org/documentation/developers/system/events/...
is it a new function?
Sorry, typo. The event is on_page_view() as per my first post. I will edit the post above to correct the typo.
Example, to load the C5 internal copy of jquery:
To add a simple scrap of script to a header (not tested code, just to show the idea)
You need to be careful with use of quotes when doing this. For a longer fragment a php variable with a heredoc <<< can be put into addHeaderItem.
Example, to load the C5 internal copy of jquery:
public function on_page_view() { $html = Loader::helper('html'); $this->addHeaderItem($html->javascript('jquery.js'));
To add a simple scrap of script to a header (not tested code, just to show the idea)
$this->addHeaderItem(' <script type="text/javascript"> alert("hello"); </script>');'
You need to be careful with use of quotes when doing this. For a longer fragment a php variable with a heredoc <<< can be put into addHeaderItem.
A couple of old threads on similar:
http://www.concrete5.org/community/forums/customizing_c5/c5-bug-or-...
http://www.concrete5.org/community/forums/customizing_c5/add-dynami...
http://www.concrete5.org/community/forums/customizing_c5/c5-bug-or-...
http://www.concrete5.org/community/forums/customizing_c5/add-dynami...
Have you seen Mnkras' howto on events...
http://www.concrete5.org/documentation/how-tos/developers/how-concr...
http://www.concrete5.org/documentation/how-tos/developers/how-concr...