0

We want to use several lead tracking snippets of code on various pages.

<script type="text/javascript">llfrmid=123456</script> 
<script type="text/javascript" src="https://formalyzer.com/formalyze_init.js"></script> 
<script type="text/javascript" src="https://formalyzer.com/formalyze_call_secure.js"></script>

...I can use drupal_add_js() but it seems to want to wrap the first line in --CDATA-- and I'm unclear if that makes a difference.

I came across this post: How to add script to <head>?. Is that accepted answer still recommended for D7? ie. just concatenating text to the header with drupal_get_html_head() Or is there a way to use drupal_add_js so it does not wrap the text in --CDATA--?

TIA

1
  • Wrapping in CDATA will not affect the inline script.
    – mikeytown2
    Commented Mar 23, 2015 at 20:41

1 Answer 1

0

You can add the JavaScript on the theme level. You added on template.php or any other template that you wish to appear the javascript.

An example is a little bit tricky because is all depend on how the theme is was created. But I can provide an example base on my theme.

I'm using a sub-theme base on Boostrap so on my page.tpl.php I would add the JS on the bottom. So your goal would be to find the footer region and add the JS below that.

<?php

?>
<header id="navbar" role="banner" class="<?php print $navbar_classes; ?>">
  <div class="container">
  <div class="col-md-3">
</div>
    <div class="navbar-header">
      <?php if ($logo): ?>
      <a class="logo navbar-btn pull-left" href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>">
        <img src="<?php print $logo; ?>" alt="<?php print t('Home'); ?>" />
      </a>
      <?php endif; ?>

      <?php if (!empty($site_name)): ?>
      <a class="name navbar-brand" href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>"><?php print $site_name; ?></a>
      <?php endif; ?>

      <!-- .btn-navbar is used as the toggle for collapsed navbar content -->
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <?php if (!empty($primary_nav) || !empty($secondary_nav) || !empty($page['navigation'])): ?>
      <div class="navbar-collapse collapse">
        <nav role="navigation">
          <?php if (!empty($primary_nav)): ?>
            <?php print render($primary_nav); ?>
          <?php endif; ?>
          <?php if (!empty($secondary_nav)): ?>
            <?php print render($secondary_nav); ?>
          <?php endif; ?>
          <?php if (!empty($page['navigation'])): ?>
            <?php print render($page['navigation']); ?>
          <?php endif; ?>
        </nav>
      </div>
    <?php endif; ?>
  </div>
</header>

<?php if ($page['Featured']): ?>
  <div class="BannerColor">
    <?php print render ($page['Featured']); ?>
  </div> <!-- /. Featured -->
<?php endif; ?>

<div class="main-container container">

  <header role="banner" id="page-header">
    <?php if (!empty($site_slogan)): ?>
      <p class="lead"><?php print $site_slogan; ?></p>
    <?php endif; ?>

    <?php print render($page['header']); ?>
  </header> <!-- /#page-header -->

  <div class="row">

    <?php if (!empty($page['sidebar_first'])): ?>
      <aside class="col-sm-3" role="complementary">
        <?php print render($page['sidebar_first']); ?>
      </aside>  <!-- /#sidebar-first -->
    <?php endif; ?>

    <section<?php print $content_column_class; ?>>
      <?php if (!empty($page['highlighted'])): ?>
        <div class="highlighted jumbotron"><?php print render($page['highlighted']); ?></div>
      <?php endif; ?>
      <?php if (!empty($breadcrumb)): print $breadcrumb; endif;?>
      <a id="main-content"></a>
      <?php print render($title_prefix); ?>
      <?php if (!empty($title)): ?>
        <h1 class="page-header"><?php print $title; ?></h1>
      <?php endif; ?>
      <?php print render($title_suffix); ?>
      <?php print $messages; ?>
      <?php if (!empty($tabs)): ?>
        <?php print render($tabs); ?>
      <?php endif; ?>
      <?php if (!empty($page['help'])): ?>
        <?php print render($page['help']); ?>
      <?php endif; ?>
      <?php if (!empty($action_links)): ?>
        <ul class="action-links"><?php print render($action_links); ?></ul>
      <?php endif; ?>
      <?php print render($page['content']); ?>
    </section>

    <?php if (!empty($page['sidebar_second'])): ?>
      <aside class="col-sm-3" role="complementary">
        <?php print render($page['sidebar_second']); ?>
      </aside>  <!-- /#sidebar-second -->
    <?php endif; ?>

  </div>
</div>
<footer class="footer container">
  <?php print render($page['footer']); ?>
    /**
        Load Custom JS
    **/
  <script type="text/javascript">llfrmid=123456</script> 
 <script type="text/javascript" src="https://formalyzer.com/formalyze_init.js"></script> 
 <script type="text/javascript" src="https://formalyzer.com/formalyze_call_secure.js"></script>
</footer>
2
  • Can you please give an answer specific to my example? TIA,
    – jchwebdev
    Commented Mar 23, 2015 at 19:23
  • I have update my answer with an example. Commented Mar 23, 2015 at 19:38

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.