I am using Omega theme in Drupal 7. And I know that you can declare default JavaScript files inside mytheme.info
. But this is globally declared and is loaded each time on every node, even on the node where I don't need the js
file. And this is resource consuming and more over is making site slower.
Is there a possibility from the theme configuration, to spec开发者_如何学Cify that js
file is only for a certain node?
In your case I would suggest adding the js from init hook in your custom modules.
mymodule_init(){
if(arg(0)=='node' && is_numeric(arg(0)) && node_load(arg(1))->type == 'my_content_type'){
drupal_add_js(path_to_my_js);
}
}
If you turn on JavaScript optimisation though I'm pretty sure it'll merge all your JavaScript files specified in mytheme.info
into a single file, which will be good for your site's performance.
It'd probably be best doing this when you're putting the site live, otherwise debugging could be a bit tricky :)
Failing that the drupal_add_js function should help you add page-specific JavaScript.
use drupal_add_js(), maybe in phptemplate_preprocess_html and wrap your condition around it .. dunno if you have the nid in the variables argument .., else use current_path()
精彩评论