Adding Javascript to Header
Permalink
Hi
I have a block that needs to add 2 separate JS files. I first just made a "js" folder in my block files and it got included automatically. Problem is now, that Concrete5 includes one of these (jquery.ui.widget.js) already when logged in (or when the toolbar appears, I'm not sure. I will have users that cannot see the toolbar).
And if it's loaded twice it creates errors.
How do I load it best just once? So it loads when concrete5 doesn't load it and otherwise ignores it.
Should I check for logged in users or a toolbar appearance or could I add it with $this->addHeaderItem in a way that concrete5 recognizes that it includes the same file twice... which it obviously doesn't recognize when load trough the JS folder.
I have a block that needs to add 2 separate JS files. I first just made a "js" folder in my block files and it got included automatically. Problem is now, that Concrete5 includes one of these (jquery.ui.widget.js) already when logged in (or when the toolbar appears, I'm not sure. I will have users that cannot see the toolbar).
And if it's loaded twice it creates errors.
How do I load it best just once? So it loads when concrete5 doesn't load it and otherwise ignores it.
Should I check for logged in users or a toolbar appearance or could I add it with $this->addHeaderItem in a way that concrete5 recognizes that it includes the same file twice... which it obviously doesn't recognize when load trough the JS folder.
Thanks. Never even heard about it before.
Guess it's that:
https://www.concrete5.org/documentation/developers/5.7/designing-for...
I'll check it out.
Guess it's that:
https://www.concrete5.org/documentation/developers/5.7/designing-for...
I'll check it out.
https://www.concrete5.org/documentation/developers/5.7/assets/overview/
Okay, so I registered my js files in application/config/app.php
But all I get is an Fatal error:
Class 'Concrete\Core\Asset\Asset' not found in /home/httpd/vhosts/xxx.com/httpdocs/application/config/app.php on line 12
...and a bugreport from April with exactly my problem:
http://www.concrete5.org/developers/bugs/5-7-3-1/overriding-assets-...
Does that mean it's non-functional yet? Is anyone actually using this?
But all I get is an Fatal error:
Class 'Concrete\Core\Asset\Asset' not found in /home/httpd/vhosts/xxx.com/httpdocs/application/config/app.php on line 12
...and a bugreport from April with exactly my problem:
http://www.concrete5.org/developers/bugs/5-7-3-1/overriding-assets-...
Does that mean it's non-functional yet? Is anyone actually using this?
I never tried registering it in app.php, but it should work.
I did use the approach described in the docs.
I can give you a link to a blog with something I'm doing right now:
http://www.anchor-ict.com/blog/overriding-minified-core-assets...
Forget about the overriding bits, but maybe there is something in it to help you.
I did use the approach described in the docs.
I can give you a link to a blog with something I'm doing right now:
http://www.anchor-ict.com/blog/overriding-minified-core-assets...
Forget about the overriding bits, but maybe there is something in it to help you.
Thanks for the link and help!
Seems that I can use "H" and "F" instead of Asset:: that works so far. Also I need to call the assets in the page_theme.php and not in the block controller. Used in the page_theme it all works, but no matter what I try in the block controller, the script just doesn't show up, even though the block is loaded.
Guess I just use the page_theme.php for now.
Seems that I can use "H" and "F" instead of Asset:: that works so far. Also I need to call the assets in the page_theme.php and not in the block controller. Used in the page_theme it all works, but no matter what I try in the block controller, the script just doesn't show up, even though the block is loaded.
Guess I just use the page_theme.php for now.
Just a quick note. You should add your javascript to the footer, not header.
First you register the asset (js)
Then you add it to a group
and where you need it (in your block), you require it
concrete5 will handle it, and will only load it once,
There is quite a bit of documentation available about this.