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

2 Answers 2

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.

4
  • I think if ((!isset($mgo) || (isset($mgo) && $mgo->type!='TYPE')) && $_GET['q']!='PATH') { would work in more cases
    – Trey
    Commented Dec 26, 2012 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
    Commented Dec 28, 2012 at 17:24
  • that's not what exactly what you asked for in the original section...brb with an addition to the answer
    – Jimajamma
    Commented Dec 28, 2012 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
    Commented Feb 8, 2013 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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.