1

I need to make asynchronous page requests on every page load using AJAX, the idea being that a function in my module is called even when pages are cached.

The only documentation I've been able to find (Drupal 7) is for form handling using AJAX and handling links.

How do I call a module function from a custom AJAX request? Would I just implement hook_menu() and write a custom ajax request or is there a better way of doing this in Drupal 7?

0

I guess the answer depends on what that ajax call does, and if it is for a 'one site use' or for a contribution module.

If it s for a contribution module hook_menu might be the only way to go.

Though if it s just for a 'one site use' i would just add a JS snippet in your page.tpl.php or even html.tpl.php that launches the Ajax query. This would work on every page even when if cached.

EDIT Actually you could also use the page preprocess function and add a js file to all your pages.

  • Yes that is the plan, although I'll still be doing it in the module... what I'm wondering is if I should write the request manually (i.e. $.get()) or can use Drupal API for this – dianovich Sep 19 '11 at 12:20
  • Well if you know how to write the Jquery yourself i would definitely do that, you ll gain a little performance and avoid a couple of headaches. – silkAdmin Sep 19 '11 at 12:33
  • I am trying to do something similar, but couldnt get hook_menu get working with ajax. as explained here drupal.stackexchange.com/questions/11330/…. any idea on this? – Srihitha Narra Sep 20 '11 at 2:00

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.