0

I've got a Javascript that I'd been able to post on every page using Drupal_add_js, and everything was fine till my client's needs changed.

Now I need to show that same JS script on every site page except one node type, and one specific URL and I dont know how to do this. I'd prefer to do it the Drupal way rather than use JS Injector if I can.

Any help is appreciated! Thanks Steph

1

Following your logic, something along the lines of...

$mgo=menu_get_object();
if ((isset($mgo) && $mgo->type=='TYPE') || $_GET['q']=='PATH') {
  ;
} else {
  drupal_add_js(...);
}

...where you are doing your drupal_add_js(...) should do it. I think this does it cleaner, but it's late in the logic day here:

$mgo=menu_get_object();
if ((isset($mgo) && $mgo->type!='TYPE') && $_GET['q']!='PATH') {
  drupal_add_js(...);
}

ADDITION:

$mgo=menu_get_object();
if (isset($mgo) && $mgo->type=='products') || strpos($_GET['q'], 'user/preference_center/ratings_reviews/')===0) {
  ; // do nothing
} else {
  drupal_add_js(....);
}

will add js only if you are not looking at a product node or not in a path that starts with user/preference_center/ratings_reviews/ BUT please note that if what is after user/preference_center/ratings_reviews is a node, this won't work because that path is actually node/#, so you will have to then do...

$mgo=menu_get_object();
if (isset($mgo) && $mgo->type=='products') || strpos(drupal_get_path_alias(), 'user/preference_center/ratings_reviews/')===0) {
  ; // do nothing
} else {
  drupal_add_js(....);
}

..instead.

  • I think if ((!isset($mgo) || (isset($mgo) && $mgo->type!='TYPE')) && $_GET['q']!='PATH') { would work in more cases – Trey Dec 26 '12 at 23:45
  • Hi Guys, I'm sorry but these replies don't work. I've used <?php $mgo=menu_get_object(); if ((isset($mgo) && $mgo->type!='products') || $_GET['q']!='user/preference_center/ratings_reviews/*'):?> test <?php endif; ?> and I'm not getting the results I'm hoping for. I need to get "test" off of node type "products" and URLs that contain the path section "user/preference_center/ratings_reviews/" – StephanieF Dec 28 '12 at 17:24
  • that's not what exactly what you asked for in the original section...brb with an addition to the answer – Jimajamma Dec 28 '12 at 20:32
  • The $mgo var didn't work for me, but I did end up using this as my guide for hiding JS from my specific pages – StephanieF Feb 8 '13 at 15:20
0

I just started working with JS & Drupal, but this might work: If you put it in the the template.php under the the YOURTHEME_preprocess_node() you can do something like:

<?php
$node = menu_get_object();
  if (isset($node)) 
  {
       if ($node->type = 'THENODETHATDOESNTNEEDIT') 
       {
       //Do some stuff
       }
       else 
       {
       //ADD YOUR JAVASCRIPT HERE
       }
  }
?> 

This should drop Javascript only into the nodes you want...

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.