Monday 26 August 2013

How to include php-generated javascript?

How to include php-generated javascript?

I've looked around and have found no great solution so far: I would like
to add customized javascript from a widget using wp_enqueue_script, but
Wordpress seems to be limiting scope to the javascript file I load in, as
my javascript file says:
Uncaught TypeError: Object [object Object] has no method 'photogallery'
Here is the output I would like to load in, as if it were its own script
via wp_enqueue_script:
<script type = "text/javascript">
(function ($) {
"use strict";
$(function () {
var $gallery = $("#gallery").photogallery(
"a",
{
thumbs: <?php echo
setBool($instance["hasThumbs"]); ?>,
history: <?php echo
setBool($instance["historyEnabled"]); ?>,
time: <?php echo
($instance["transitionTime"] * 1000); ?>,
autoplay: <?php echo
setBool($instance["autoplayEnabled"]); ?>,
loop: <?php echo
setBool($instance["isLooped"]); ?>,
counter: <?php echo
setBool($instance["hasCounter"]); ?>,
zoomable: <?php echo
setBool($instance["zoomable"]); ?>,
hideFlash: <?php echo
setBool($instance["hideFlash"]); ?>
}
);
});
}(jQuery));
</script>
I know that manually coding the default values in to another js file, and
using enqueue_script will work fine, but how do I do this properly, with
custom widget values, and not load the required js file twice? I am not
interested in complex ajax requests unless it is my only option, as
mentioned here.
Thanks in advance.

No comments:

Post a Comment