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.

Write a Comment

Comment