Issue with drupal_add_js function not functioning properly within the hook_user_login implementation

In my Drupal 7 project, I am using drupal_add_js to add JavaScript within the hook_user_login function.

Although drupal_get_js confirms that my JavaScript has been added, it seems to disappear when the page is loaded.

This is my code snippet:

function popups_user_login(&$edit, $account) {
    $js = 'some inline js here';
    drupal_add_js($js, array('type'=>'inline', 'weight'=>1));
    drupal_add_js(drupal_get_path('module', 'popups').'/popups.js', array('weight'=>2));
}

The purpose of this module is to display a popup once the user logs in under certain conditions.

You can find a similar discussion on drupal.org here.

I am puzzled by why the JavaScript disappears and would appreciate any insights or help.

(I have tested this on two different Drupal 7 installations and encountered the same issue on both sites)

Answer №1

As mentioned by Clive, the issue was caused by a redirect immediately after logging in. To verify this, one can install Devel and enable "Display redirection page" within the Devel settings.

The workaround involves setting a session variable within hook_user_login (containing the nids of desired popups) and then retrieving it in hook_page_alter to inject the required JS code accordingly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing a Vue.js v-bind:style attribute onto a dynamically generated element post-page initialization

Let me start by explaining my current issue and dilemma: I have been tasked with converting an existing JS project into a Vue.js framework. While I could easily solve a particular problem using jQuery, it seems to be posing quite a challenge when it comes ...

Navigating the React-file-viewer conundrum

I recently utilized FileViewer from the 'react-file-viewer' module. import { FileViewer } from 'react-file-viewer'; Unfortunately, I encountered the following error: Could not find a declaration file for module 'react-file-viewer& ...

What are the best strategies for interpreting and learning from JavaScript frameworks documentation?

Coming from a background in .NET, I find it challenging to navigate documentation for JavaScript frameworks. Let's take the "Node.js MongoDB Driver API" as an example. Within this API, there is a Collection object that contains a method called count() ...

Having trouble resolving the templateUrl for AngularJS in a shared module

It's really frustrating me. I have an angular module that is injected into different apps. Whenever I attempt to use templateUrl with a relative path in the shared module, it throws a 404 error. angular.min.js:99 GET http://localhost:8001/modals/mo ...

Removing a row from a table in AngularJS connected to FireBase

I am currently facing an issue where the function fails to work when attempting to delete a row. My goal is to be able to remove a specific row from the table by clicking on the red button. Here is the link to my plunker code In index.html, I have includ ...

Load data from a MySQL Database into a DataTable

I am currently in the process of developing a CRUD application. With a large dataset stored in a MySQL database, my intention is to utilize a JQuery DataTable instead of manually creating a table. However, the issue I am facing is that while the table appe ...

javascript cannot utilize html reset functionality

My drop down menu includes an onChange event that triggers a JavaScript method. However, when I select a new value and then click the reset button, the dropdown reverts back to its original value but the onChange event does not fire. <select onChange= ...

Tap to smoothly scroll through each block using the animate() function

ul { padding: 0; margin: 0; height: 180px; overflow: auto; } li { height: 50px; background: pink; list-style: none; margin-bottom: 10px; height: 50px; ...

Tips for selecting the appropriate cssSelector value within the Chrome browser using the Developer Tools (F12) feature

What is the process for selecting a cssSelector() in Chrome Browser using Chrome Developer Tools? Could you provide an example for the given code snippet below? WebElement searchBox = driver.findElement(By.cssSelector("selector")); searchBox.click(); ...

Adjust the Highcharts semi-pie design by eliminating the gap between the pie and the legend

I'm having trouble adjusting the spacing between the bottom of a semi circle donut chart in Highcharts and the legend below it. Despite my efforts, I have not been successful in reducing this gap. Here is the basic chart I am currently working on: h ...

Retrieving modified object values in Node.js - A comprehensive guide

How can I retrieve the modified value of an object? The file index.js is executed before index2.js and here's what my code looks like: var object = { size:'5' }; var setSize = function(size) { object.size = size; } exports.object ...

Utilizing Raycaster for modifying the Box's face color upon clicking

I've been experimenting with the raycaster in order to select faces of cubes that are created, and then change the color of the clicked face. So far, I've managed to make it work for selecting entire objects, but not individual faces of those obj ...

Using JSON in Highcharts: Customizing Border and Label Colors

Currently using Highcharts in JSON format with the following syntax: var neutral_color = '#c4c4c4', medium_grey = '#929292'; lineChartJSON['chart']['plotBorderColor'] = medium_grey; lineChartJSON['chart&ap ...

The propagation of SVG events from embedded images via the <image> elements

I'm currently developing a diagramming tool using HTML, CSS, and Javascript with SVG for the drawing canvas. This tool consists of predefined "building blocks" that users can place on the canvas, rather than allowing free-hand drawing of shapes. Each ...

`options for exporting images in Highcharts`

I have implemented the code below to export a png: var added_chart_options = { credits:{ enabled: true }, title:{ style:{ display: 'block' } }, subtitle:{ style:{ di ...

Steps on creating a two-column layout with a responsive breakpoint

I want to design a div that includes two texts. The initial text should be placed in the first column and expand into the second column if it's lengthy. The second text should be positioned above the expanded text from the first column. Here is an ex ...

Can TypeScript be used to dynamically render elements with props?

After extensive research on SO and the wider web, I'm struggling to find a solution. I have devised two components, Link and Button. In short, these act as wrappers for <a> and <button> elements with additional features like chevrons on t ...

Angular 12: An issue has occurred due to a TypeError where properties of undefined cannot be read, specifically pertaining to the element's 'nativeElement

Within my phone number input field, I have integrated a prefixes dropdown. Below is the code snippet for this feature. HTML code in modal <div class="phone" [ngClass]="{ 'error_border': submitted && f.phoneNumber.er ...

I need assistance with this ajax/javascript/php

I am currently working on creating a dynamic chained list. The idea is to have the user make selections from the first four dropdown lists, and based on their choices, a fifth dropdown list should appear. However, I am facing some issues with my code as th ...

Passing parameters to Next.js pages

Here is my function: export async function getServerSideProps({ req }: any) { const user = ( await axios.get("http://localhost:4000/api/auth/status", { withCredentials: true, headers: { Cookie: `connect.sid=${req.cookies["c ...