Javascript works fine, but i need to get my_var variable from module. At the moment its not working.

Clear my page cache and browser cache several times.

The variable 'my_var' is showing value on front end but not on backend/admin side.

Front end theme is Bartik; the admin theme is Seven.

All other variables are available on Admin side from Drupal.settings, except the one I pass from my custom module.

This works well on both themes.


This is my code.


 * Implements hook_page_build().
function MY_MODULE_page_build(&$page) {

    // HERE it is calling the js
    drupal_add_js(array('MY_MODULE' => array('my_var' => 'MY_VALUE'),), 'setting');
    drupal_add_js(drupal_get_path('module', 'MY_MODULE') . '/js/MY_MODULE.js');


(function ($) {
    Drupal.behaviors.MY_MODULE = {
    attach: function(context) {

On front end, it alerts with test followed by MY_VALUE, but on admin side, it shows test; Firebug then shows me:

Uncaught TypeError: Cannot read property 'my_var' of undefined

How can I fix this?

  • 1
    where are you written drupal_add_js function ?
    – Rupesh
    Apr 26 '14 at 14:53
  • @Rupesh In custom module called MY_MODULE.module
    – Zafar S
    Apr 26 '14 at 14:59
  • 1
    which function or any hook ?
    – Rupesh
    Apr 26 '14 at 14:59
  • in hook_page_build()
    – Zafar S
    Apr 26 '14 at 15:00
  • There are nothing wrong in this code. Check may be some typo error.
    – Rupesh
    Apr 26 '14 at 15:03

calling drupal_add_js in hook_init is a fairly standard response to this kind of question, as is hook_boot.

hook_init is used for setting up things required by the page - but doesnt run on cached pages.

hook boot is used for code that has to run on cached pages as well as uncached pages.

The way I usually debug this kind of thing is with the javascript console. In chrome, open the console and type


hit enter and it should print out the whole settings object for you to inspect. This will let you see what you have.

I suspect the problem may be down to when hook_page_build gets fired on admin pages if Im reading your question correctly.

Uncaught TypeError: Cannot read property 'my_var' of undefined 

means that


evaluates to undefined. It could be at any level. so try the javascript console trick and see what you find (is Drupal there.. is drupal.settings there).

you could also try

(function ($) {
    Drupal.behaviors.MY_MODULE = {
    attach: function(context,settings) {

which is more keeping with how it is documented for behaviours on drupal.org


your problem is here

    drupal_add_js(array('MY_MODULE' => array('my_var' => 'MY_VALUE'),), 'setting');

you should wrote

drupal_add_js(array('MY_MODULE' => array('my_var' => 'MY_VALUE')), array('type','setting'));
  • @kenorb instead of down vote take a better look at it(your shared link) if (empty($javascript)) { $jquery_added = FALSE; } // Construct the options, taking the defaults into consideration. if (isset($options)) { if (!is_array($options)) { $options = array('type' => $options); } } else { $options = array(); } if (isset($options['type']) && $options['type'] == 'setting') { $options += array('requires_jquery' => FALSE); } $options could be array !!!
    – Yuseferi
    Sep 22 '16 at 12:39
  • Could you clarify that? Also how does it solve the issue.
    – kenorb
    Sep 22 '16 at 12:52
  • @kenorb you down vote and comment he $options is a string, not an array but if you have a better look at drupal_dd_js function you could see finally settings check as a array element with type key. So your comment is mistake !!! is it right?
    – Yuseferi
    Sep 22 '16 at 12:55
  • Yes, I've removed invalid comment, but your answer doesn't explain how does it solve the issue, as I believe both syntax can be used as per: if (!is_array($options)) { $options = array('type' => $options); }. Also I cannot take DV, if you won't re-edit your post. So if you could clarify that, would be great. If you're not sure whether it fixes the issue or how, you should post a comment instead.
    – kenorb
    Sep 22 '16 at 13:59
  • drupal_add_js(array('MY_MODULE' => array('my_var' => 'MY_VALUE')), array('type','setting')); is wrong.
    – apaderno
    Mar 5 '19 at 17:31

This is likely not the "preferred" way of handling this but I just use php to output js code thusly:

if (isset($post)) {
    echo "<script type='text/javascript'>\n";
    foreach ($_POST AS $name => $value) {
        echo "var " . $name . " = '" . $value . "';\n";
    echo "function initialize() {\n";
    if (strpos($_SERVER['REQUEST_URI'],'results') !== false) {
        echo "initializePillars();\n";
        echo "initializeFeed(instagramFeed);\n";
    }else if (strpos($_SERVER['REQUEST_URI'],'quiz') !== false) {
        echo "initializeQuestion();\n";
    echo "}\n";
    echo "</script>\n";

It's easy and it works.

Not the answer you're looking for? Browse other questions tagged or ask your own question.