0

Ive put this code before the closing head tag in my html.tpl.php but nothing is happening. Im using the zen theme. Ive tried clearing my cache.

Im I somehow missing a syntax error or is this a Zen/ Drupal qwerk?

<script>
    $(document).ready(function() {
        alert('hi');
    });
</script>

</head>
4

It would be helpful to know what version of drupal you are using.

Modifying html.tpl.php to add Javascript like this is really a bad choice as it is a very brittle way of coding something.

Jquery in Drupal 7 is loaded using the noConflict mechanism. This avoids javascript namespace collisions:

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict():

If you wanted to use code like you've written you would write:

<script>
    jQuery(document).ready(function() {
        alert('hi');
    });
</script>

You're better off adding a scripts[] entry into your theme_name.info file and placing such code in its own javascript file in your theme directory. Drupal will load it into the HTML <head> for you and aggregate your javascript files for you if you have aggregation turned on.

# In YOURTHEME.info
scripts[]= js/example.js

/* Example /sites/all/themes/YOURTHEME/js/example.js */
jQuery(document).ready(function($) {
  $('nav ul.primary-menu').append('<div class="nav-blackhole"></div>');
});
  • html.tpl.php is not used before Drupal 7. I hope the OP is not using that with Drupal 6. :) – kiamlaluno Dec 15 '12 at 1:27

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.