Issue: I have an input filter that, when triggered, executes drupal_add_js. However, when I view source of the page, I see this command issued twice or more times.

Question: Is there something I can do to prevent duplicate javascript commands on a page?

Code sample: Below is the sample of the input filter that I'm writing. In some pages, for an unknown reason

function _js_slider_process($text, $filter, $format, $langcode, $cache, $cache_id)

        preg_match_all("/\[slideshow:(#[\S]+)\]/", $text, $matches);

        // If no match return and be done.
        if (!isset($matches[1])) {
            return $text;

        // gather information
        $text_to_remove = $matches[0];
        $elementid = $matches[1];

        // remove filter trigger text
        $text = str_replace($text_to_remove, '', $text);

        // Do we have any matches located in the firs match
        if (isset($elementid[0])) {
            // Inject javascript file
            drupal_add_js(drupal_get_path('module', '_js_slider') . '/' . _JS_SLIDER_JS_FILE_SRC, 'file');

            // Inject options into a javascript variable
            $js_slider_options = array(
                'first' => variable_get('_js_slider_control_first', _JS_SLIDER_HTML_CTRL_FIRST),
                'prev' => variable_get('_js_slider_control_prev', _JS_SLIDER_HTML_CTRL_PREV),
                'next' => variable_get('_js_slider_control_next', _JS_SLIDER_HTML_CTRL_NEXT),
                'last' => variable_get('_js_slider_control_last', _JS_SLIDER_HTML_CTRL_LAST),
                'ctnl_placement' => variable_get('_js_slider_control_placement', _JS_SLIDER_HTML_CONTROL_PLACEMENT)

            drupal_add_js(array('_js_slider' => $js_slider_options), 'setting');

            // Call it
            drupal_add_js('jQuery(document).ready(function () { a = new SlideShow("' . $elementid[0] . '"); a.init(); });', 'inline', 'footer');

        return $text;

Safest and most straightforward way is to move js file to module's info file, and include it always. just make sure it works only on elements it should, and on all of them. For this you need appropriate classes.

Other way is to use ['#attached']['js'] of highest possible element, instead of calling drupal_add_js manually. if you will set script twice, you will override older instance, so no problem.

If, for reasons unknown to me, you decide you have to call drupal_add_js yourself, use drupal_static to make sure you didn't call it already. It will require least changes in existing code - and in my opinion is worst solution. But will work. Typical usage is:

$mycounter = &drupal_static(__FUNCTION__, 0);

That makes $mycounter shared among all scopes where it is called, and initialized to 0. Then, you can test if it's still 0, and if it is - add your JS and increment counter with good old $mycounter++


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.