3

I am trying to add a simple footer.js to my page. I created a new block and put it in the footer. I then use PHP snippet in the body of the block with the PHP code:

<?php
drupal_add_js('footer.js');
?>

When viewing the source code, footer.js is there but when I click on it, it is a 404 error (not found). The footer.js is located under: /var/www/drupal/sites/www.example.com/footer.js. Then I tried:

<?php
drupal_add_js('footer.js', array('type' => 'file', 'scope' => 'footer'));
?> 

I don't think that is working, either. I think the issue is with the path. Any ideas, or do I have to write a custom module?

1 Answer 1

1

It is most likely a path issue, you should be able to use the DRUPAL_ROOT constant to get the correct one:

$path = DRUPAL_ROOT . '/sites/www.example.com/footer.js';
drupal_add_js($path);
5
  • Great! Now when I click on the footer.js via the source code of the web page, it opens up the javascript file. Now, is the javascript file the standard type (i.e. <script type="text/javascript"></script>), or is it that weird jQuery format that Drupal wants for inline (i.e. (function ($) {... })(jQuery);)
    – user785179
    Commented Jan 22, 2012 at 1:51
  • It's the second one, that wraps your javascript so it's nice and jQuery context-safe
    – Clive
    Commented Jan 22, 2012 at 1:53
  • Is there any way to bypass this jQuery format and use pure and strict javascript syntax? jQuery makes no sense to me, and my code I want to embed is in javascript. In Drupal 6, it was simple to just copy/paste javascript code directly into the footer and voila - it worked! Looks like they took this out in Drupal 7
    – user785179
    Commented Jan 22, 2012 at 4:26
  • If its just normal js just put that directly in the external file (no script tags) and it will work. Do me a favour and up vote the answer, some idiot down voted it even though it's correct! There's a lot of unfulfilled people out there with nothing better to do!
    – Clive
    Commented Jan 22, 2012 at 13:27
  • I was able to get it to work by finding a module. What I wanted to do was embed a Piwik javascript code in the footer of my webpage. Apparently the process to embed javascript is so complicated that Piwik has a Drupal 7 module for it, BUT, I did learn from their module that you can put 'inline' javascript code. That is good to know. Anyhow, thank you for your quick response and suggestions. You did ultimately answer my question of finding the javascript file, although I could not get the javascript to work. Thanks!
    – user785179
    Commented Jan 23, 2012 at 0:18

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.