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(); }
Javascript:
function autoupdate() {
//alert('autoupdate is running');
jQuery.ajax(
{
type: "POST",
url: "/autoupdate",
cache: false,
success: function(data) {
var result = JSON.parse(data);
jQuery("div.autorefresh").fadeIn("slow").html(result['html']);
}
});
//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));
exit();
}
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?
drupal_add_js()
, specifically the 'setting' option