I have a behavior as follows:

Drupal.behaviors.mymodule = {
  attach: function(context, settings){
      console.log(" user id and other fields "+settings.user.id);


How could it be achieved?

  • What about passing the variables you need by using drupal_add_js (settings) in your custom module?
    – rreiss
    Aug 17, 2013 at 16:03
  • how would drupal_add_js(settings) interact with my behavior? Aug 17, 2013 at 17:03
  • See the examples on Drupal's API. drupal_add_js
    – rreiss
    Aug 17, 2013 at 17:48
  • Yes, that is what I wanted, please compile your suggestions into an answer and I will mark it as correct. Thanks a lot. Aug 17, 2013 at 18:18

1 Answer 1


You should use Drupal's drupal_add_js function with the 'settings' option (more info).


global $user;
$uid = $user->uid;
drupal_add_js(array('myModule' => array('uid' => $uid)), 'setting');
  • Note that this won't work if you try to display hostname. The hostname will be cached per-user so all anonymous users will share the same IP.
    – Nux
    Dec 18, 2017 at 15:24

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.