2

I wanted to remove some JavaScript files, since they conflict with my custom JavaScript on my mobile theme.

I tried with the following code, but it is not working in hook_preprocess_page(), which I think it really works in the first place; it's just that the original initialization of $scripts in page.tpl.php loads after hook_preprocess_page() alteration.

// Load all js scripts
    $scripts = drupal_add_js();

  // Unset all unnecessary scripts
    unset($scripts['module']['sites/all/modules/ajax/ajax.js']);
    unset($scripts['module']['sites/all/modules/contribs/mollom/mollom.js']);
    unset($scripts['module']['sites/all/modules/contribs/views_slideshow/js/jquery.cycle.all.min.js']);
    unset($scripts['module']['sites/all/modules/contribs/views_slideshow/contrib/views_slideshow_singleframe/views_slideshow.js']);
    unset($scripts['module']['sites/all/modules/contribs/views_slideshow/contrib/views_slideshow_thumbnailhover/views_slideshow.js']);
    unset($scripts['module']['sites/all/modules/quicktabs/js/quicktabs.js']);
    unset($scripts['module']['sites/all/modules/panels/js/panels.js']);
    unset($scripts['module']['sites/all/modules/tipsy/javascripts/jquery.tipsy.js']);
    unset($scripts['module']['sites/all/modules/tipsy/javascripts/tipsy.js']);
    unset($scripts['module']['sites/all/modules/boxes/boxes.js']);

    // Recreate the template variables

    $vars['scripts'] = drupal_get_js('header', $scripts);

How can I remove JavaScript files from the list of JavaScript files load when my theme is used?

  • 1
    Did you consider that you might need to rewrite your custom JS so that it does not conflict with Drupal's JS? If you succeed in removing these scripts, there may be unintended consequences; a lot of Drupal's core and contrib modules use JS to enhance the UI. Have you read the (drupal.org/node/121997)[documentation on using javascript in Drupal 5 & 6] to make sure you are building your JS correctly? – sheena_d May 10 '12 at 14:54
  • As far a I know every piece of code conforms the Drupal code standard. My issue here is I have desktop version and mobile version of the site with different theme fo each version. Most of the modules I'm using are packed with JS and CSS which these modules are intended for desktop site only 'coz the mobile site is just blogroll and dont need those files to be loaded every page load. – ninjascorner May 11 '12 at 5:30
1

This how solved my problem.

Found out that jquery_update uses hook_theme_registry_alter() to unset jquery in Drupal core and replace a new version of Jquery. So this preprocess function is being load after everything all. What I did is I created a hook_theme_registry_alter() and update the weight of the module higher than jquery_update module so I can override the jquery_update_theme_registry_alter().

This jquery_update_theme_registry_alter() prevents any alteration in javascript which a big headache if you're working on some adjustments like in my case.

Code:

function my_module_theme_registry_alter(&$theme_registry) {
    global $theme;

    // Only apply this code in mobile theme
    if($theme == 'myTheme') {
    //Attach the my_module_preprocess_page in the theme registry preprocess functions
      $theme_registry['page']['preprocess functions'][] = 'my_module_preprocess_page';
    }
}

function my_module_preprocess_page(&$variables) {

    // Perform the logic if either jQuery Update's jquery.js is newer than core's.
    if (variable_get('jquery_update_replace', TRUE)) {
        // Get an array of all the JavaScript files loaded by Drupal on this page.
        $scripts = drupal_add_js();

        // Replace jquery.js first.
        $new_jquery = array(jquery_update_jquery_path() => $scripts['core']['misc/jquery.js']);
        $scripts['core'] = array_merge($new_jquery, $scripts['core']);
        unset($scripts['core']['misc/jquery.js']);

        // Loop through each of the required replacements.
        foreach (jquery_update_get_replacements() as $type => $replacements) {
            foreach ($replacements as $find => $replace) {
                // If the file to replace is loaded on this page...
                if (isset($scripts[$type][$find])) {
                    // Create a new entry for the replacement file, and unset the original one.
                    $replace = JQUERY_UPDATE_REPLACE_PATH .'/'. $replace;
                    $scripts[$type][$replace] = $scripts[$type][$find];
                    unset($scripts[$type][$find]);
                }
            }
        }
        // Unset javascripts that is/are not needed in mobile theme
        unset($scripts['module']['sites/all/modules/boxes/boxes.js']);

            .....

        // Recreate the scripts variable
        $variables['scripts'] = drupal_get_js('header', $scripts);
    }
}

Hope this will help others. Please let me know if this approach is a violation. Thanks

1

Checkout AdvAgg. Has hooks for you to use and plays nicely with jquery update.

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.