Skip to content

Instantly share code, notes, and snippets.

@wimleers
Last active December 17, 2015 19:18
Show Gist options
  • Save wimleers/5658972 to your computer and use it in GitHub Desktop.
Save wimleers/5658972 to your computer and use it in GitHub Desktop.
/**
* Parse the user ID from the Drupal.visitor.user.uid cookie.
*
* @return Number
* The user id: nonzero if it is an authenticated user, zero if it is the
* anonymous user, in which case the Drupal.visitor.user.uid cookie value will
* not exist either.
*
* @see user_user_login()
* @see user_user_logout()
*/
Drupal.getUserID = function () {
var userID;
function parseUserID () {
var values = document.cookie.split(';');
for (var i = 0; i < values.length; i++) {
var parts = values[i].trim().split('=');
if (parts[0] === 'Drupal.visitor.user.uid') {
return parseInt(parts[1], 10);
}
}
return 0;
}
if (userID === undefined) {
userID = parseUserID();
}
return userID;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment