Community Documentation

Adding Javascript snippets

Last updated September 23, 2011. Created by JuliaKM on September 23, 2011.
Log in to edit this page.

This section is incomplete. Please add to it by posting a comment or editing this page.

There are several ways to add JavaScript snippets to your site. They can be embedded into an existing html page, added to an existing .js file, or added as separate files which are called when the HTML page loads.

Adding JS as a separate, external file

In this method, you upload the JavaScript to your site as a individual file, and add a PHP code snippet to a .php file. (When a visitor views the page, this PHP snippet creates HTML code which makes their browser load the .js file). The PHP snippet will look something like this:

<?php
print drupal_add_js('path-to/javascript.js', 'theme');
?>

This method is often used to load .js files by adding that PHP snippet to the end of template.php in your site's theme directory.

See also: How can I add a Javascript? http://drupal.org/node/333056

Troubleshooting JavaScript Snippets

In FireFox, use Tools > Error Console. Clear the error console and then look at it when you load the page. For example, it will let you know if an external .js file didn't load because you got the path wrong. Note: some errors can be safely ignored.

nobody click here