Advice for using a javascript library in multiple places
Permalink 1 user found helpful
I'm using a 3rd party javascript library in a few custom templates for a few different blocks. At first I was placing the javascript library in the js folder for the first custom template, but now that I have a variety of templates that will be using it, I don't want to store multiple copies of the library.
Where should I store this javascript? In the js/ folder at the root of the installation? Also, how do I add this javascript to the header?
Where should I store this javascript? In the js/ folder at the root of the installation? Also, how do I add this javascript to the header?
Thanks. I was hoping there was a better way, maybe using an "addHeaderItem" method, but I might end up using this method.
Yes. Like I said not sure if this is the most elegant way for Concrete5. But its been working for me. Not a problem!
If you load the scripts using addHeaderItem or addFooterItem, with the HTML helper, there is an optional third array parameter ($uniqueItemHandle) to give a handle for the item you are loading and a version.
Items added with the same handle will only be loaded once, from the most recent version, irrespective of where the actual files are loaded from.
So multiple addon packages can each include their own copy of a javascript library and only one copy will ever be loaded with a page.
The pull request (now approved and closed) can be found at:
https://github.com/concrete5/concrete5/pull/470...
Another approach is to add guards in javascript to wrap the code, as described in this howto:
http://www.concrete5.org/documentation/how-tos/developers/protect-a...
If you search back through the forums for addHederItem and Fancybox (because Fancybox was the jquery plugin that was giving lots of duplicate loading issues) you will find much discussion on this about a year or 18 months ago.
Items added with the same handle will only be loaded once, from the most recent version, irrespective of where the actual files are loaded from.
So multiple addon packages can each include their own copy of a javascript library and only one copy will ever be loaded with a page.
The pull request (now approved and closed) can be found at:
https://github.com/concrete5/concrete5/pull/470...
Another approach is to add guards in javascript to wrap the code, as described in this howto:
http://www.concrete5.org/documentation/how-tos/developers/protect-a...
If you search back through the forums for addHederItem and Fancybox (because Fancybox was the jquery plugin that was giving lots of duplicate loading issues) you will find much discussion on this about a year or 18 months ago.
Thanks. If I'm determined to store my 3rd party library in a single location, where would you suggest I store it?
I have added it to the root of the installation in a directory called "js_custom". However, the following addHeaderItem code within the template's view.php isn't working:
Is there something obvious I'm doing wrong?
I have added it to the root of the installation in a directory called "js_custom". However, the following addHeaderItem code within the template's view.php isn't working:
$html = Loader::helper('html'); $this->addHeaderItem($html->javascript("/js_custom/jquery.isotope.js"), 'VIEW');
Is there something obvious I'm doing wrong?
If it is for a single site, personally I would create a package for all my common code, javascript etc, then create a library or helper interface to that.
For the root script folder, you probably don't need the 'VIEW', just leave that out.
For the root script folder, you probably don't need the 'VIEW', just leave that out.
I removed 'VIEW' from the above code, but it still isn't working. For now I'll just do DAker's suggested method, but I would prefer to get addHeaderItem working. Most examples I've seen of addHeaderItem are done within the controller rather than the view. Maybe that's the issue? But if that's the case, I wouldn't be able to add header items specific to the template.
Anyway, thanks a lot for trying to help.
Anyway, thanks a lot for trying to help.
You can add this command to your header file for your theme.
Not sure if it's the best way with Concrete5, but i find it easy enough.