in Web & Tech

Loading Drupal SimpleAds’ JavaScript with drupal_add_js

I’m working on a site that utilizes the Drupal module SimpleAds. In an effort to optimize front end performance I moved all my theme’s Javascript to the Footer (by simply moving <?php print $scripts; ?> around in html.tpl.php). After doing that the ads disappeared and my browsers console telling me Uncaught ReferenceError: _simpelads_load is not defined.

The problem is that Simple Ads now tries to load some of its scripts before its dependencies because it wasn’t injected properly using drupal_add_js. Moehac found a solution and thankfully posted it to the issue queue:

Copy simpleads_block.tpl.php to your theme and change

  <script type="text/javascript">
    _simpelads_load('.simpleads-<?php print $tid; ?><?php if ($prefix) : ?>-<?php print $prefix; ?><?php endif; ?>', <?php print $tid; ?>, <?php print check_plain($ads_limit); ?>);
  </script>

to

  <?php
     drupal_add_js("_simpelads_load('.simpleads-" . $tid . (($prefix) ? "-" . $prefix : "") . "', " . $tid . ", " . check_plain($ads_limit) . ");", array('type' => 'inline', 'group' => JS_THEME));
  ?>

If you don’t copy & paste please mind the typo in _simpelads_load!

From my understanding of Drupal coding standards this is the way it should have been done in the first place anyhow.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)