Including (or not) javascript files from /concrete/js
Permalink
As I find myself adding another js file to a template that is already in the /concrete/js directory, I wonder if anyone has devised a smart way to not duplicate files when not needed. This time it is the jquery.scrollTo.js file that I need, which is in the core but not often (ever?) called in the 'header_required' stuff. Before that, it was jquery.metadata.js and before that it was jquery.ui.js.
I hate to duplicate files in the head when I don't need to, as it obviously adds to the page load. But I need these files to be always available, whether logged in or not, admin or not, etc. Has anyone figured out a way to include js files only when c5 doesn't include them already?
I hate to duplicate files in the head when I don't need to, as it obviously adds to the page load. But I need these files to be always available, whether logged in or not, admin or not, etc. Has anyone figured out a way to include js files only when c5 doesn't include them already?
I should've specified that I'm adding this in a theme template. So far, what you're suggesting isn't adding jquery.ui.js to the head.
I figured there was a method like this, but I still can't find a good explanation of it in the docs.
I figured there was a method like this, but I still can't find a good explanation of it in the docs.
that should work fine, what path is it printing in the header
It doesn't seem to be printing anything.
Yeah, this is a problem. Adding header items from the template has no effect because c5 sets up the header items before the template is rendered so by the time you call addHeaderItem it's too late.
See this thread for a discussion of this issue:
http://www.concrete5.org/community/forums/customizing_c5/conflictin...
Basically, the only solution we could come up with is to load the javascript via javascript -- that way you can check if the jquery object you want already exists or not, and if not, use jquery's $.getScript() function to load the script dynamically.
HTH
-Jordan
See this thread for a discussion of this issue:
http://www.concrete5.org/community/forums/customizing_c5/conflictin...
Basically, the only solution we could come up with is to load the javascript via javascript -- that way you can check if the jquery object you want already exists or not, and if not, use jquery's $.getScript() function to load the script dynamically.
HTH
-Jordan
OK, that makes sense. I had tried already loading my scripts via javascript, but things were getting tangled up with the load order and some scripts firing when their dependencies hadn't yet loaded. I didn't try using $.getScript() though, so I think I will work with that and see if I can work it out.
Thanks!
Thanks!
I spoke too soon. Your post made me think that maybe I just needed to put my addHeaderItem call before my Loader::element('header_required') call, and voilà! jquery.ui.js is now magically printed along with the rest of the header. I don't know why that didn't occur to me before.
I haven't read the other thread you linked there, so I don't know what that's about but maybe it's a separate issue.
I haven't read the other thread you linked there, so I don't know what that's about but maybe it's a separate issue.
Well I'll be -- I had no idea... glad you got it figured out without having to resort to javascript trickery!
will pull the js from the concrete/js folder, and it will only pull it once per page