I use drupal 6 and ubercart2. Loading of cart and checkout page takes a while so I am thinking about removing any js and css files which are not used on specific pages. What would be the ideal way to do it? I know about js agregation but want to try the first approach. Thank you very much.
3 Answers
The AdvAgg module can do this fairly easily. Syntax for this is different between 6.x & 7.x. This is the hook syntax for 6.x. Works very similar to hook_css_alter() & hook_js_alter() in Drupal 7. Using these hooks is how to do it in D7 BTW.
hook_advagg_css_pre_alter(&$css, $preprocess_css, $public_downloads) {
// Remove css files.
}
hook_advagg_js_pre_alter(&$js, $preprocess_js, $public_downloads) {
// Remove js files.
}
Have a look at the FOAD technique to get rid of stylesheets and scripts. It happens in your own theme.
You can take an alternate route and use stuff like Stylestripper module or Mothership base theme.
With reference to your question 'optimal way to remove unwanted js and css files from page', hope this approach works ::
1) Include your js & css files only under appropriate hook as per applicable
2) Use syntax like -
drupal_add_js (drupal_get_path ('module', 'module_name') . '/js/js_file_name.js' ); drupal_add_css(drupal_get_path('module', 'module_name') . '/css/css_file_name.css');
3) You can make use of syntax like this to set some settings variable & check in your js file before invoking your event inside the behavior
In module file ::
drupal_add_js (array ('module_name' => array ('variable_name' => some_value)), 'setting' );
In js file ::
Drupal.behaviors.testName = {
attach: function (context, settings) {
if(typeof Drupal.settings.module_name != 'undefined'
&&
Drupal.settings.module_name.variable_name == some_value){
// Do SOMETHING
}
}
};
4) If your code already included js & css files at places which could be done a better way, then you need to make the code changes accordingly. For example,
If we could include the js & css files in hook_node_view (based on D7 standard, say), then we should not include it from hook_init.
Hope it helps