7

In Drupal 7, you can use the following code to attach a script to a particular content type:

function THEMENAME_preprocess_node(&$variables) {
  if ($variables['type'] == 'INSERT_CONTENT_TYPE') {

        // include javascript
        drupal_add_js(path_to_theme() . 'INSERT_SCRIPT');

      }

    }

What code do you need to use to attach a script to a particular Node ID or a specific View?

I have tried swapping variables['type'] with various items, but none seem to work.

  • 2
    Where are you adding this code? Some preprocess function? – Shoaib Nawaz Oct 6 '11 at 19:45
  • Hi I have updated my answer to make it clearer – big_smile Oct 7 '11 at 17:23
14
+50

Node ID:

function THEMENAME_preprocess_node(&$variables) {
  if ($variables['nid'] == 'INSERT_NODE_ID') {
    // include javascript
    drupal_add_js(path_to_theme() . 'INSERT_SCRIPT');
  }
}

View:

function THEMENAME_preprocess_views_view(&$variables){
  if ($variables['name'] == 'INSERT_VIEW_MACHINE_NAME') {
    // include javascript
    drupal_add_js(path_to_theme() . 'INSERT_SCRIPT');
  }
}
  • Thanks! This gets the bounty as it is the cleanest solution and works for both node and view. – big_smile Oct 9 '11 at 21:12
  • I'm pretty sure that you need a "/" before "INSERT_SCRIPT". (e.g. /scripts/custom.js) – Chris Happy Dec 11 '16 at 8:22
1

If you are using D6. Try using JS injector to insert custom javascript to a page or view.

  • Hi I am using Drupal 7 and would like to do it without an extra module. – big_smile Oct 7 '11 at 17:26
0

Try this:

function THEMENAME_preprocess_node($node) {
    if ($node->nid == INSERT_NODE_ID) {
        // include javascript
        drupal_add_js(path_to_theme() . 'INSERT_SCRIPT');
    }
}
0

If I understand well your question, why not display your script accoring the alias of your view or your node or view? (also is possible using the node ID)

I have tested these functions with _preprocess_node (nodes) an also with _preprocess_page (for views and nodes) in Drupal 6 and 7.

this is for node with ID with _preprocess_node:

function seven_preprocess_node(&$vars) {
    if(isset($_GET['q'])){
        $nodeID = '9';
          if(arg(1) == $nodeID){
              drupal_add_js(path_to_theme() . '/test.js');
          } 
      }
} 

this is for node with alias with _preprocess_node:

function THEMENAME_preprocess_node(&$variables) {
    if(isset($_GET['q'])){
          $path = drupal_get_path_alias($_GET['q']);
          if(preg_match('#test_page#', $path)){
              drupal_add_js(path_to_theme() . '/test.js');
          } 
      }
}

and this function with _preprocess_page will be useful for nodes and/or views (alias):

function THEMENAME_preprocess_page(&$variables) {
    if(isset($_GET['q'])){
          $path = drupal_get_path_alias($_GET['q']);
          if(preg_match('#test_page#', $path)){
              drupal_add_js(path_to_theme() . '/test.js');
          } 
      }
}

this last function is only to show an example of a function seeking alias of your view/node or node ID:

function THEMENAME_preprocess_page(&$variables) {
    if(isset($_GET['q'])){
        $script = '/test.js';//script name
        $currentPath = drupal_get_path_alias($_GET['q']);//current alias
        $targetPath = '#test_view#';//alias to match (delimiter is #), is for example of your view's page.
        $targetNodeID = '9';//node ID to match
          if(arg(1) == $targetNodeID || preg_match($targetPath, $currentPath)){
              drupal_add_js(path_to_theme() . $script);
          }
     }
}

I hope information be useful.

0

nothing worked for me, so i checked with 'preprocess' hook, that works fine as follows:-

function MYMODULE-or-THEME_preprocess(){
    if(isset($_GET['q'])){
    $nodeID = '232'; //NODE ID TO CHECK
          if(arg(1) == $nodeID){              
     drupal_add_js(drupal_get_path('module', 'MYMODULE').'/js/myscript.js');
         } 
    }
}

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.