Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I wanted to unset some javascripts since they does nothing but conflict on my custom JS in my mobile theme.

What I did:

// 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);

But the above code is not working in 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 the preprocess_page alteration.

Anyone of have an idea?

share|improve this question
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

3 Answers

up vote 2 down vote accepted

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

share|improve this answer

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

share|improve this answer

You can try drupal_static_reset('drupal_add_js') it will remove all js check this link also I tried this before to remove colorbox only drupal_static_reset('colorbox'); and it worked fine with me

share|improve this answer
1  
drupal_static_reset() is only available in Drupal 7. Also, there's no function that registers itself with drupal_static() using the identifier 'colorbox' (even in the Colorbox module itself), so your second example above will do nothing. – Clive May 10 '12 at 11:49
As Clive said drupal_static_reset() only work in D7..and my question is for Drupal 6. – ninjascorner Jun 1 '12 at 4:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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