In your theme’s template.php, we only need to add a couple lines to the theme_preprocess_node function ($variables may be $vars, depending on your theme):
drupal_add_js(array('nid' => $variables['node']->nid), 'setting'); // adds the node id
drupal_add_js(array('title' => urlencode($variables['node']->
title)), 'setting'); // adds the node title
var node_nid = Drupal.settings.nid; // get the node id
var node_title = decodeURIComponent(Drupal.
settings.title.replace(/\+/g, ' ')); // get the node title
The .replace() changes “+” symbols back to regular spaces, which were needed when passing the value. decodeURIComponent() does the rest.
Pay special attention to the difference between setting and settings. ‘setting’ is used when adding a setting (php), ‘settings’ is used when calling the settings (js). It makes sense, just confusing when you don’t think about it.
That’s it! If this began churning your brain, I can imagine you’re already thinking about how you can use this for other data and instances (other functions within template.php for example). If that’s the case, we’d love to hear how you may have used this in your work. Comments are welcome!