How to add javascript to one node in drupal 7?

How to add specific javascript to specific node in drupal 7? Right now I am adding my javascript.js through the "javascript librairies" module. But that adds .js to all nodes. I want to enable myjavascript1.js for node 1 and myjavascript2.je for node 2?

Is there any way to achieve this? Should I create my own module?

+4
source share
3 answers

Create node--525.tpl.php separate template file, replace 525 with your node id, and then add javascript to the section of the chapter.

Add this code in the template file

 <?php function YOURTHEME-NAME_preprocess_page(&$vars, $hook) { if ((arg(0) == 'node') && (arg(1) == 'add' && arg(2) == 'CONTENT-TYPE')) { $vars['template_files'][] = 'page-node-add-CONTENT-TYPE'; } } ?> 
+1
source

You can create a node file - [nid] .tpl.php for a specific node identifier, after which you can use your specific js file for a specific node id.

+1
source

You can use https://drupal.org/project/scriptfield (shameless self-defense takes place here). This will allow you to do exactly what you need, based on node.

This is most useful if you need to do this with more than one node, as this will allow you to do this without creating a single template for node id.

0
source

All Articles