views:

145

answers:

1

I'm working on a module and am trying to add some javascript to the next page a user sees after logging in or out. Calling drupal_add_js() on hook_user (op == login) doesn't seem to work; I'm assuming this is because drupal_goto is called after the login is completed and a fresh page request is initiated.

I've considered using hook_user to set session variables which I can then respond to on the next page load but that seems somewhat fragile. Any suggestions?

+2  A: 

If you want something to be carried over to a new page you only have a few options:

  • Alter the url.
  • Store in the database.
  • Store in session.

Altering the url, would probably be quite hard and messy. Storing in the session or database is basically the same thing. So you would probably want to use the Drupal session system instead of making your own.

You could add something in the session and then in hook_init check for it and if it's there add the js and delete it from the session.

I don't think you will find a much better solution, though it would be nice if there were.

googletorp
Thanks for the response. Yeah...I think I'm going to store it in the session and use hook_init to add the js but not delete the trigger from the session at that point. Instead I'll have the JS make an ajax call to delete the trigger from the session so that I know for sure the JS has executed. Hopefully we're not overlooking some in-built Drupal way of handling this elegantly; I'd love an option in drupal_add_js for the js to persist til the next full page load.
Aaron
I've been fiddling with this and unfortunately it's not working. user_logout calls session_destroy right before running the hook (see here: http://api.drupal.org/api/function/user_logout/6) which prevents me from saving anything to the session. Even running session_start before saving to $_SESSION doesn't fix it. I've also tried sess_write and sess_read with no luck (though I've read things that indicate I shouldn't use those anyway). Any suggestions?
Aaron
I ended up sticking the trigger in the URL by altering $_REQUEST['destination']. I don't love it but it's the best I could come up with. Thanks!
Aaron