Executing javascript in Drupal after node is posted

The day are busy at Hoist. We are ready to launch our new work tool very soon (workshops are held next week with few spots left—let me know if you're interested).

Working on the user interface goodness I needed to execute some javascript immediately after a node has been posted in Drupal. The javascript should not be included on every node view, only the first. The nodeapi's 'insert' operation can't be used to include javascript because there is a redirect after it is processed and the 'view' operation can't tell if a node is brand new or not.

The solution I used is to set a session variable. It feels a little hackish, but it works great.

function yourmodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'insert') {
    $_SESSION['yourmodule'][$node->nid] = $node->nid;
  }
  if ($op == 'view' && $_SESSION['yourmodule'][$node->nid]) {
    // Insert call to drupal_add_js() here.
    unset($_SESSION['yourmodule'][$node->nid]);
  }
}

No comments

Add your comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.

Recent photos

About the blog

This is the personal website of Andreas Haugstrup Pedersen: commentary on media, communi­cation, culture and technology. Read more»