I'm working on a module that among other things, needs to open a popup window on a user login. The module is called 'inthooks'.

I use hook_login() in my module and then inside I call drupal_add_js() to add the code, but so far I tried inline or file and nothing's working. I checked the final DOM after login and the Javascript code or file reference is nowhere to be seen

This is the code I got so far:

function inthooks_user_login(&$edit, $account) {
drupal_add_js(drupal_get_path('module', 'inthooks') .'/inthooks.js', array('type' => 'file'));
}

There is an inthooks.js file in the module folder that so far only gives an alert so I know it's executing, but nothing happens. In there I have

jQuery(document).load(function (){
	alert("Hook done");
});

Should I be using another function other than document.load? I tried simply having the alert line in there without the jQuery but it doesn't work.

So far the only way I could get the alert to show is by drupal_set_message()'ing the return of drupal_get_js(). This is the only way I can get the JS to appear, but obviously is not the way to go.

Can someone help me in what I'm doing wrong, or how would I achieve what I want to do?

The only clue I have so far is that after login there is a redirect and the actual DOM where I'm adding the JS alert is lost, but I can't confirm this as it's only a hunch

I also tried putting the drupal_add_js() in the hook_init function for my module so theoretically the alert would appear every time a page is loaded, but nothing happens either.

Comments

jaypan’s picture

Try changing this:

jQuery(document).load(function (){
alert("Hook done");
});

To this:

Drupal.behaviors.myModule.attach = function()
{
  alert("Hook done");
});

No guarantees you will solve your problem, but this is the Drupal way of doing executing an onload function.

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

vj’s picture

hi

If you want a popup on user login page then you should use hook_form_alter to include your js.

also you can try this and clear cache

function example_init(){
drupal_add_js('alert("Hello!")', 'inline');
}
neskenazi’s picture

Thanks for the replies

@Vj

How would I use hook_form_alter? isn't that for altering the login form itself? I want to execute javascript after the form is submitted, because I actually need the user and password that was typed.

I'm asuming you mean I should use 'inthooks_form_user_login_form_alter', but this includes the JS code before the form is submitted, doesn't it_

@Jay

I don't think I understand very well how the Drupal.behavior functions, since when I include the code as you put it, it returns an error that "Drupal.behavior.inthooks" is undefined (I supposed that I should replace myModule for the module name)

I actually changed the code, and the one that works is

jQuery(document).ready(function () {
	alert("Hook done");
});

But still this only works on hook_init and happens on every page load (and actually before the hook_login, so I don't have access to the username and password entered)

jaypan’s picture

You should be hooking properly into the Drupal system with your javascript, using the Drupal.behaviors object. There are reasons for this - for example when an element on a page is refereshed through #ajax, or when code you may be writing is called in a modal popup. If your code is not part of the Drupal.behaviors object, it doesn't properly re-fired when another script calles Drupal.attachBehaviors().

If you were getting an error with the Drupal.behaviors object, you should show your code so that we can help you get it proper, rather than bypassing the system using $(document).ready().

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

dahousecat’s picture

All of these answers seem to be focusing on changing the JS, when you have already said the JS is not being added to the DOM at all.

I am also using hook_user_login and calling drupal_add_js.

If I call drupal_get_js immediately after drupal_add_js then I can see it is there but by the time the page is loaded the js has gone.

Can anyone confirm if there is a known issue with calling drupal_add_js from hook_user_login?

jaypan’s picture

All of these answers seem to be focusing on changing the JS, when you have already said the JS is not being added to the DOM at all.

That's because these problems are very often a case of incorrect javascript (by not using the wrapper function, or having some other error in the code), which people incorrectly diagnose as the code not being added to the DOM.

To answer your question, hook_user_login() is used during submission to save values to the database. It is not a hook that is used during any page display. I'm actually reasonably sure (though I haven't tested it right now) that a redirect happens after hook_user_login(), to prevent users from getting a message about data being needed to be resubmitted when they refresh the page. Since there is a redirect after hook_user_login(), your javascript won't be added to the DOM, as the page the user sees after hook_user_login() is a fresh page load, not the page that that hook was called upon.

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