I would like to add javascript on logout. I tried using hook_user_logout but the javascript doesn't get added.

Even adding something as simple as drupal_set_message doesn't result in a message being displayed.

I know the hook is firing because I can add a watchdog message in the hook and see it in the logs. The watchdog message is about the only thing I tried that DID work. Curious about other options that may or may not have results, I tried looking at Actions and Triggers and Rules. I basically decided to try to do something simple on logout, like add a message. But even those modules don't work! I was actually really suprised when Rules didn't work.

I can add messages and do actions elsewhere. Has anyone else had problems with hook_user_logout? And does anyone know if it is possible to add javascript with that hook?

Is this some sort of problem involving drupal_goto? Hmmm. I am on the verge of just adding JQuery to every page to check every link to see if it is a logout link and then adding a click event. It will work for my purposes, but it is such a hack. I would prefer not to.

  • What do you mean by "add javascript on logout", really? You want to include JS file on the first page user will encounter after logging out? What exactly are you trying to achieve?
    – Mołot
    Commented Oct 9, 2014 at 7:22
  • Well, it's a personal project I was working on. I was writing a drupal module to connect to a restful api service, and authentication is sent via client side. My strategy is to use a jwt token and store it in local storage and use that to authenticate with the api. The jwt would be set on login, and then get erased on logout. Everything was working great until I hit the logout point. Commented Oct 9, 2014 at 7:38
  • I suppose I could store the token in $_SESSION or something and add it to settings on page load .. but... bleh.. I wanted to use local storage, gosh dang it Commented Oct 9, 2014 at 7:44

1 Answer 1


Even adding something as simple as drupal_set_message doesn't result in a message being displayed.

That's the expected behaviour - in the logout hook, you're still inside the authenticated user's session. Once they're logged out, you're not any more. So messages intended for that user are no longer there, because that session has been destroyed.

You should implement a logout/token expiry resource in your API, and invoke that in the logout hook. Then you don't need to remove the token held in local storage, as it will not be valid any more.

  • Hey that sounds like a good idea. I'll need to look into that. Commented Oct 9, 2014 at 8:15

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.