I can't figure out how to insert a javascript function inside a node.

It's a countdown script that uses an external js file and i need the function to be called from within a node. (each node will have a different countdown target date)

I set the input filter to php but it didn't work. All it does is printing the js file content as node content

I put the js file in a folder inside sites/all/scripts and here's how i use :

<?php include('sites/all/scripts/countdown.js'); ?>

<script type="text/javascript">
y: 2012; m: 5; d: 25; h: 10; m: 0; s: 0;

<div id="countdown"></div>

Any ideas ?

thanks a lot

  • <?php include('sites/all/scripts/countdown.js'); ?> This snippet would never work any way, AFAIK. What you appear to be doing here is instructing Drupal to write the output of the js file into the page. So what you should get is just javascript source in plain text added to the contents of the node. – Drupac May 31 '12 at 22:25

Javascript happens on the client-side so there's no need to use PHP at all here. Using the PHP filter is in fact strongly discouraged as it introduces potential security risks.

To include javascript in an HTML page you use the <script> tag like so:

<script type="text/javascript" src="/sites/all/scripts/countdown.js"></script>
  • I know i only used the php filter to point at the right js file. I tried using the script tag but it didn't help neither I even added the js file to the .info file so that it is loaded in every page but it doesn't work – amstram May 13 '12 at 19:42
  • I'd guess there's something wrong with your javascript then. Have a look at this bit: y: 2012; m: 5; d: 25; h: 10; m: 0; s: 0;...that's not valid javascript code – Clive May 14 '12 at 8:16
  • You're right i copy/pasted the wrong portion. The code i try to use has equal signs instead of colons. And it works when used in a plain html file. - Thank you for your time ! – amstram May 14 '12 at 11:39

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.