Widget + Javascript
Submitted by jenelso2 on Fri, 11/14/2014 - 11:48
Forums:
Had an interesting scenario in my development of widgets. I needed to be able to load up a lightbox (or really any javascript library) for a particular widget, in this case, a lightbox style image gallery.
I didn't need the lightbox javascript libraries included on every page, nor did I need or want the $(document).ready() jQuery call to fire for this widget on every page. My solution to this, was to have the Jadu team add a custom include for us at the very end of the document template where I could load any custom development I needed for a particular page. This looked like:
<!-- BEGIN: WSC FOOT -->
<?php if (isset($fancyBox) && $fancyBox == true) { ?>
<script type="text/javascript" src="/site/custom_scripts/includes/js/fancybox/fancybox.js"></script>
<script type="text/javascript">
custom code to display the fancybox based on values created in widget php code
</script>
<?php } ?>
<!-- END: WSC FOOT -->
The widget code looked like this:
<?php
$fancyBox = true;
$fancyBoxSetup = array('bunch','of','fancybox','settings','for','gallery');
?> gallery HTML goes here
This allows me to only include the javascript I need/want for the pages that actually need it and lessens the amount of widget specific javascript that gets loaded on every page load.
So, questions I have are:
Is there a better way to do this that anyone can see?
Have I missed a better method that anyone else is using?
Is there a best-practice from Jadu on how to implement something like this?
Thanks - Jeremy
General Concept:
Jadu Product:
Jadu Version:
