hi to all,
i want to add a javascript when an user is getting logged in. I'm using hook_user in this way:

function mymodule_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case "login":
drupal_add_js(drupal_get_path('module', 'mymodule') .'/mymodule.js', 'module');
...

but it doesn't work and i dont know the way. But if i add that drupal_add_js instruction into hook_init, it's works, but js will be loaded for each page and it isn't what i want.

do you know to workaround this problem? i want to check something after the login, not in every page.
thank you

Comments

nevets’s picture

When do you want to include the JavaScript? On what pages, under what conditions?

TuTToWeB’s picture

i want to add js after log in, in any page it brings the user under no conditions...
simply when a login is performed

nevets’s picture

Ok what do you want the js to do?

I ask because I suspect the reason you are having a problem is once login is successful there is a page redirect (which is why adding in hook_user() for $op equals 'login' does not work).

TuTToWeB’s picture

in my local server there's no any redirect, but in my online server there's a redirect to an other page.
js should show me a simple alert when a condition is true or false. The assert is given by JQuery...i want to simply load this js file after login, regardless of there is an redirection or not. Is it possible? Is there a workaround for that?

thank you so much

nevets’s picture

Technically I believe there is always a redirect (even if you end up on the same page) once you complete the form.

So what are you trying to test for. In case you are not aware you could use drupal_set_message() to place a message in the message area. This would work in hook_user() when $op equals 'login'.

TuTToWeB’s picture

i know drupal_set_message api function and in hook_user works too, but i must use an alert for that application

pcambra’s picture

Did you manage to solve this?
I want to add a drupal setting using drupal_add_js in hook_user and it doesn't seem to be working.

jaypan’s picture

hook_init()
{
  if(!user_is_anonymous())
  {
    // do stuff on all pages for logged in user
  }
}

Contact me to contract me for D7 -> D10/11 migrations.

brahimmouhamou’s picture

You can create a $_SESSION variable in the hook_user() and do a preprocess_page($vars) where you check if the session variable exists and add your drupal_add_js.