I'm trying to add 'new' or 'updated' to the links in a Menu, and one of the ways is with AJAX. But to do that, I need to add AJAX to existing node/page path following the documentation and various examples, hook_menu() successfully adds the scripts and call backs for paths that only exist in the module file

// The page that will run the command.
$items['my-custom-path/trigger-page'] = array(
  'title' => 'AJAX trigger page',
  'page callback' => 'my_ajax_trigger_page',
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK,

[my_ajax_trigger_page calls add_ajax_js and works with and without clean URLs]

but when I add items for an existing node with a menu path, a page I created, and gave a URL path like 'my-toc-page' does not get registered for the AJAX calls. nothing happens - the scripts to not load on to the page at all, and the callbacks silently fail I have also tried this with a View, with AJAX enabled, but still it does not register

// register script load
$items['test-toc-page'] = array(
  'title' => 'MyTable of Contents',
  'page callback' => 'add_ajax_js',
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK,

How do you do this on a page that already exists and has content and blocks on it? No forms, but the links on the page need to be processed.

Using a page template file , I can load the JavaScript files onto an existing page, but the AJAX call backs fail.

Thanks everyone, this is a continuation of my initial question about AJAX on page load. Existing Page not getting registered by hook_menu to trigger Drupal AJAX on page load

1 Answer 1


There are many ways to add scripts to an existing page, one option is:

 * Implements hook_init().
mymodule_init() {
  if ($some_condition) {
    drupal_add_js(drupal_get_path('module', 'mymodule') . '/mymodule.js');

hook_menu() probably isn't going to help you here, because each menu router item can only have one page callback.

  • Thank you Dylan, that may save me the template step. Even when I'm able to get the script to load, last time with a template, it still does not work with the AJAX callback, registering the callback method for an existing path/node/page is working only for 'fake' paths which are often used for forms, from what I've seen, not AJAX'ing existing pages and menus.
    – omcode
    Apr 9, 2013 at 14:15
  • Could you add more detail about what's not working? Specifically, try using the debugger and network panel in your browser to find out what part of your script is executing, where it fails, and what the error is. Also, I'm not sure what you mean by "fake" paths, I've never heard of this in Drupal.
    – Dylan Tack
    Apr 9, 2013 at 18:03
  • the 'fake' paths are not pages created in Drupal CMS, the are only paths in the hook_menu call. What is not working here is that the JavaScript makes the AJAX call, and fails because there is nothing on the server to process it. What I really need to find out is How do you register an AJAX call back for an Existing Page, why does it only seem to work with one that is created solely in hook_menu, there must be a way to user hook_menu or some other method to wire up the call back from a page already created in Drupal, and with a set path like 'Drupal_Site/my-page'.
    – omcode
    Apr 10, 2013 at 11:52
  • Router items defined by hook_menu aren't fake at all, they are every bit as "real" as a page created in the CMS. You'll need a separate hook_menu URL and callback to handle the AJAX call (see api.drupal.org/api/drupal/… for an example). There's also an ajax_example module you can study in drupal.org/project/examples.
    – Dylan Tack
    Apr 10, 2013 at 20:49
  • Thank you Dylan, I was using that word to try and explain the difference between those paths and pages that were already existing. I'd used the patterns in that example, and will review them again to see why existing pages are not hitting the call backs.
    – omcode
    Apr 11, 2013 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.