0

This is my first time working with Drupal (Drupal 7).

I've created a jQuery plugin which populates a target div. The target div is added to the node via panelizer.

I only want to run the script for when it's needed. Just now it runs all the time. Even on the dashboard, this causes script errors.

I'm not sure what the best approach is for this.

EDIT (SOLUTION):

Thanks for the input. I decided to use a Javascript approach.

var elementExists = document.getElementById('target');

if (typeof(elementExists) != 'undefined' && elementExists != null) {
  // Script
} else {
    return;
}
  • you can write your script as block and display it when needed and exlude for admin – Paul Bönisch Sep 15 '15 at 11:59
2

From my experience it's usually best to leave this check to JS and don't bother on PHP side. Of course it all depends on your definition of best, but benefits I see are:

  • It's easy and stable to test for div existence with $("#myid"), opposed to more mysterious tests in PHP. Now you know that this div will appear when X is true and Y == Z. Will you remember that in two months? Sure, we have comments. But will future developer understand what you meant? Will you be disciplined enough to write them, and do it really clearly?

  • It's easier for caching - if you aggregate your scripts, you will use the same aggregated file for pages with and without div, so one less check / download for browser

  • It lessens the chance for conflict between your module and other ones.

    • Modules shouldn't just overwrite [#attached] array, but I encountered some that did.
    • If you are using drupal_add_js instead, making sure test for conditions is run when it is supposed to might be hard, if other modules will try to reorder hooks. So this isn't a sure way to avoid problem described above.
    • Various caching systems might miss the conditions you use. Probably nothing as severe as Boost and Mobile Tools issue, but risk is there.
    • Theme shouldn't need to know about blocks - if you will leave test to theme, point above will not apply, but you risk crashes whenever theme is switched or block is disabled. Testing for it on both sides will add a lot of code.
    • And I'm sure there are other things I simply never encountered yet.

    Of course you could say most of those are bugs in other modules, and you would be right, but being right does not help if you need your site up and running.

  • It's easier to quickly see scripts added via .info file than to search code for drupal_add_js or attached or... you see, there are many ways to do it in code, happy hunting if you are new developer and need to find where this one script came from.

  • For the first point, that is why we have comments in code. :) Could you expand the point about module conflicts? I think that would be an interesting one, since most of the users would not think of that. – kiamlaluno Sep 15 '15 at 16:21
  • @kiamlaluno added some. Best I could do this evening. – Mołot Sep 15 '15 at 20:12
1

As alternative solution you may use tracking code. That would allow you to load JS or anything else only on specific pages. It would also give you overview what and where you run extra stuff.

Like this:

enter image description here

1

In template.php add this code:

function mytheme_preprocess_html(&$variables) {
  $theme_path = path_to_theme();
  $path = drupal_get_path_alias();
  if($path == 'my/path') {
      drupal_add_js($theme_path . '/js/example.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.