Adding JavaScript and CSS to every page in Drupal without using hook_init()

The problem of adding css or javascript inside a hook_init() implementation, is that the hook is called during _drupal_bootstrap_full(), and will run in cases where we are not delivering HTML (such as AJAX requests). Even if this does not have any visible effect, as the html is never outputted in these requests, it unnecessarily penalises them.

Use hook_page_build() or hook_page_alter() instead:

<?php

// Bad:
/**
  * Implements hook_init().
  */
function yourmodule_init() {
  drupal_add_js(drupal_get_path('module', 'yourmodule') . '/yourmodule.js');
}

// Good:
/**
  * Implements hook_page_build().
  */
function yourmodule_page_build(&$page) {
  drupal_add_js(drupal_get_path('module', 'yourmodule') . '/yourmodule.js');
}

Source: https://twitter.com/davereid/status/294554866649542657