0

I wanted to add script code to implement "LiveChat" functionality on just three pages of our website. So, I wanted to add one script code given by our LiveChat agency into three webpages of our website.

Script Code

<script src="http://www.examp.com/scripts/invitation.ashx?company=4444"></script> 

So, I have created three separate html node templates based on their node ID.

Theses three custom node templates are under

/sites/all/themes/mytheme/templates along with (node.tpl.php and html.tpl.php)

  1. (contact page) html--node--28.tpl.php

Url of this page: www.example.com/contact

  1. (admissions page) html--node--1212.tpl.php

Url of this page: www.example.com/admissions

  1. (market page) html--node--378.tpl.php

Url of this page: www.example.com/market

Code used in all custom node templates (html--node--28.tpl.php, html--node--1212.tpl.php, html--node--378.tpl.php) is the same:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
  "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language; ?>" version="XHTML+RDFa 1.0" dir="<?php print $language->dir; ?>"<?php print $rdf_namespaces; ?>>
<head profile="<?php print $grddl_profile; ?>">
  <?php print $head; ?>
  <title><?php print $head_title; ?></title>
  <?php print $styles; ?>
 </head>
<body<?php print $attributes;?>>
<?php print $scripts; ?>
//Adding script for LiveChat
<script src="http://www.examp.com/scripts/invitation.ashx?company=4444"></script>
  <div id="skip-link">
    <a href="#main-content" class="element-invisible element-focusable"><?php print t('Skip to main content'); ?></a>
  </div>
  <?php print $page_top; ?>
  <?php print $page; ?>
  <?php print $page_bottom; ?>
</body>
</html>

But, I am not able to see my script (<script src="http://www.examp.com/scripts/invitation.ashx?company=4444"></script>) when I debugged it in view pagesource.

Thanks!

  • Just to double check cus you didn't mention doing it, did you clear you cache after adding these template files? – Jance Jun 11 '15 at 20:23
  • Yes, I have cleared cache – Vidushi Jun 11 '15 at 20:31
  • If you make a change to your html.tpl.php file in that containing folder, do your other pages get changed? And do these pages change? – Jance Jun 11 '15 at 20:39
  • Yes, if I add this <script> code into my html.tpl.php file then I can see changes in all the pages. But, if I add this script in three node id pages, then I am not able to see any change. – Vidushi Jun 11 '15 at 20:46
1

Instead of adding three template files for three nodes with the same content in it in my opinion will be a better solution to add the JS file conditionally using drupal_add_js

For eg:


 $allowed_nid = array(28, 1212, 38);
 if(in_array(arg(1), $allowed_nid)){
  drupal_add_js("http://www.examp.com/scripts/invitation.ashx?company=4444", "external");
 }

You can add the snippet in a hook_init function in any of your custom modules. (Best is add in a custom utility module which hold all general function to be used throughout.) Else you can add it in your theme's template.php in the template_preprocess_page function.

  • Thanks Malabya .. Where should I add this function? Should I add it in template.php file? – Vidushi Jun 12 '15 at 13:07
  • @Vidushi Have updated the answer with it. :) – Malabya Tewari Jun 12 '15 at 14:33
  • Awesome.. Thanks. Instead of just three nodes, Can we make it available for under entire 'Admissions' sections? I mean all the subpages of 'Admissions' section? – Vidushi Jun 12 '15 at 19:19
  • What do you mean by subpages? As per my understanding from your question its better to have 3 separate pages. – Malabya Tewari Jun 12 '15 at 20:33
  • Yes, that is correct. But, now my client wants it on all the admissions pages as well. If 2.0 Admissions, 2.1 - About admissions, 2.1.2 - Contact us, 2.1.3,2.1.4, 2.3,2.4... So, all the subpages of Admissions along with three different pages. – Vidushi Jun 12 '15 at 20:44
0

You can do this to your html.tpl.php

<?php $node = menu_get_object();
      if ($node && $node->nid == NID):?>

<script>
<--! Your script goes here -->
</script>

<?php endif;?>

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.