4

In my current Drupal 6 installation, a lot of Javascript scripts are being loaded (core scripts, jQuery, modules, etc).

I only have one script in my theme that I need, so I want to only include that file and get rid of everything else.

What's the best way to accomplish this?

2

In your template.php you can override the generation of $vars['scripts'] to be just your file.

However, I suspect that this is a really bad idea and may have a lot of consequences.

  • And how can I do that? I've tried several ways, but none worked. – brpinto Jun 13 '11 at 15:54
2

You can put something like this in your template.php:

function phptemplate_preprocess_page(&$vars) {
  drupal_add_js($theme_path . '/js/your_js.js', 'theme'); 

  $js = drupal_add_js();
  unset($js['core']); 
  unset($js['module']); // or
  unset($js); // but then it would also clear the drupal_add_js above
  $vars['scripts'] = $js;
}
1

Drupal ships jQuery as a default JavaScript library. It is not included to the system unless a JavaScript code is added by recommended way using:

  • Adding an entry to scripts array in theme info (ini) file
  • Calling drupal_add_js()

A number of other scripts are also added by core and modules.

You can manage to stop them by overriding phptemplate_preprocess_page function in your themes' template.php

If you want to include a single JS file due to performance penalty of loading a lot of script you can turn on Optimize JavaScript files at example.com/admin/settings/performance. After enabling it you will have to flush cache in order the changes to take effect.

  • Turning on the optimize Javascript files feature only concatenates all scripts and reduces the overall size, but still loads a lot of code I don't want... :\ – brpinto Jun 13 '11 at 18:58
  • Ya that is true. But I added the last block if you are adding your own script an optimized composed form of all other scripts. :) – Shoaib Nawaz Jun 13 '11 at 19:25
  • As others have pointed out, doing that is a bad idea. You will not be able to install new modules that add JS unless you manually go through that and add it to your theme. That is the thing when using a framework. There is a lot of code and only a part of that applies to your site. But you don't go and remove all PHP code in Drupal and the modules you're using that doesn't affect your site either, do you? – Berdir Jun 14 '11 at 7:59
  • Yes it is true that framework comes with a lot of code base that we have to include in our solution. But at some extent you can trim down it in case of Druapl. Drupal is modular and you can disable what is unwanted. Drupal executes only what is required in a request cycle. If your content page is already cached then Drupal would not re-generate it until you force it or schedule to rebuild, that results a fast retrieval. – Shoaib Nawaz Jun 14 '11 at 13:48
0

This is a pretty old issue but I can't really see a good solution on here so I thought I'd write one.

Basically what you need to do in your template.php is use hook_js_alter($js) then use devel (or what ever you prefer to debug with) to see what the $js array has in it.

Unset what you do now want to have included. Be aware that you'll lose quite a lot of functionality if you remove drupal.settings.

I would advise keeping jQuery. If you must use another version of jQuery use the jquery update module (http://drupal.org/project/jquery_update), or if you are importing JS from another source then just make sure that your version is loaded above where drupal.settings is loaded.

0

Drupal only has one sanctioned mechanism for removing specific assets, regardless of the motivation for doing so.

For CSS:

hook_css_alter

For JS:

hook_js_alter

To remove all style and scripts except .info style and script use below codes:

function hook_css_alter(&$css) {
    $themes=list_themes();
          foreach($css as $key=>$item) {
            if (!in_array($key, $themes['yourthemename']->stylesheets['all'])) {
                unset($css[$key]);
            }
        }
}



function hook_js_alter(&$js) {
    $themes=list_themes();
    foreach($js as $key=>$item) {
        if (!in_array($key, $themes['yourthemename']->scripts)) {
            unset($js[$key]);
        }
    }
}

If the theme/module is custom, then obviously you can just remove the offending code manually if that makes more sense.

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.