Adding Javascript to Header


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.

View Replies: View Best Answer
WillemAnchor replied on at Permalink Reply
Take a look at the 5.7 asset system

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.
Kiesel replied on at Permalink Reply
Thanks. Never even heard about it before.

Guess it's that:

I'll check it out.
WillemAnchor replied on at Permalink Best Answer Reply
Kiesel replied on at Permalink Reply
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/ on line 12

...and a bugreport from April with exactly my problem:

Does that mean it's non-functional yet? Is anyone actually using this?
WillemAnchor replied on at Permalink Reply
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:

Forget about the overriding bits, but maybe there is something in it to help you.
Kiesel replied on at Permalink Reply
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.
Steevb replied on at Permalink Reply
Just a quick note. You should add your javascript to the footer, not header.