EDIT: There seems to be a ready-to-use http://drupal.org/project/block_refresh module available as @albertski indicated, so no need to struggle to develop a new module.

I've implemented the code on https://stackoverflow.com/questions/8144436/refreshing-a-div-in-drupal to get a Drupal 7 node page division auto-updated. Here is the code outputting simple time() function:

Hook menu:

$items['autoupdate'] = array(
'title' => 'Resize',
'page callback' => 'block_update',
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,

Function itself:

function block_update(){
$html = time();
print drupal_json_encode(array('html' => $html)); exit(); }


    function autoupdate() {
  //alert('autoupdate is running');
    type: "POST",
    url: "/autoupdate",
    cache: false,
    success: function(data) {
      var result = JSON.parse(data);
  //alert('autoupdate is complete');
setInterval("autoupdate()", 2500);

However, instead of time() I would like to use node variables and having troubles to make the module to see $node. Even if I put my div on a node page and call menu_get_object() the javascript sees only the content /autoupdate page:

function block_update(){
  $html = arg(0);
  print drupal_json_encode(array('html' => $html));

shows: autoupdate

I tried to change the path in both hook menu and javascript from autouptade to node/%node/update, but unfortunately javascript doesn't want to recognize %node as argument. I am afraid it recognizes only static URLs. But then how is it possible to pass node ID to javascript?

  • See drupal_add_js(), specifically the 'setting' option
    – Clive
    Sep 23, 2014 at 13:39

1 Answer 1


I probably would have done this differently but than again I'm not a hundred percent sure what you are doing.

In response how can you pass the node id to javascript. Perhaps in hook_page_alter():

$node = menu_get_object();
if ($node) {
  drupal_add_js(array('your_module_name' => array('nid' => $node->nid), 'setting');

Then in your ajax code get the nid via:

  • unfortunately I don't have hook_page_alteration(), instead I've placed the autorefresh div in a block: function server_jobs(){ $block = array( '#prefix' => '<div class="autorefresh">', '#suffix' => '</div>', '#markup' => time(), ); return $block; } So the method suggested doesn't work in my case. Can you elaborate exactly how differently you'd do this?
    – Sasha
    Sep 23, 2014 at 21:32
  • Sorry meant hook_page_alter(). Edited code above. Without putting too much thought I probably would have looked if I can use this module drupal.org/project/block_refresh (or see how it is implemented).
    – albertski
    Sep 24, 2014 at 19:16

Your Answer

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

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