I think should be simple, but i get the error "Drupal.settings undefined"

I created a module, and in the hook_init I just do this:

 $js_settings = array(
    'prueba' => 'randomtext',
  );

  drupal_add_js(array('pruebajs' => $js_settings), 'setting');
  
  drupal_add_js($path.'/js/prueba.js','module','header') ;

And when I try in the "prueba.js" to get the data like this:
var s = Drupal.settings.pruebajs ;

It does not work. Debugged with firebug it says Drupal.settings.pruebajs is undefined

($path is the path to my module)

To make sure the script was executed in the script I just wrote "alert('boooo')" and it worked, but when I try to get the settings, it just fails.

May you help me and please, let me know what I am doing wrong?

Thanks in advance.

Comments

ttkrpink’s picture

I have same problem. If you find the answer after 3 years, can you post it? Thanks lot

jaypan’s picture

Show your code.

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

sirajs’s picture

Hi, I'm having the same problem. Has anyone figured out what is going on?
I'm developing a theme for Drupal 7.12

Goal: Pass a php variable from template.php (or html.tpl.php/page.tpl.php) into a javascript variable in .js file

Currently, in my mytheme.info file I added a javascript file:
scripts[] = js/script.js

In my template.php file I do the following: (I have tried copying this function into my html.tpl.php/page.tpl.php files too with no success.)

function mytheme_preprocess_html(&$vars) 
{
    drupal_add_js(array('mytheme' => array('somevar' => 'HELLOWORLD')), 'setting');
}

In my script.js file I have the following code

(function($){
 $(document).ready(function() 
 {  
    alert(Drupal.settings.mytheme.somevar);
 });
})(jQuery);

The error: Drupal.settings.mytheme is undefined is thrown.

I have also tried the following in my template.php file:

function mytheme_preprocess_html(&$vars) {
  drupal_add_js("jQuery.extend(Drupal.settings, {'somevar':'HELLOWORLD'});", 'inline');  }

and the following in my script.js file:

(function($){
 $(document).ready(function() {  alert(Drupal.settings.somevar); });
})(jQuery);

This code actually produces an alert box but displays the value 'undefined' in the box.

I also checked the rendered HTML output with firebug and the SCRIPT headers do not contain the added line of code. Nor does any other location in the HTML output or the resultant DOM contain the variables I have declared.

Any help would be greatly appreciated! Thanks!

jaypan’s picture

It may be because you aren't using the proper Drupal onload behaviors - the Drupal.settings object may not yet be defined. You should be using this:

(function($){
  Drupal.behaviors.myModule = {
    attach: function()
    {
      alert(Drupal.settings.somevar);
    }
  };
})(jQuery);

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

sirajs’s picture

Thanks Jay. Sorry about the slow response ... I do appreciate your reply. I just got around to looking at this again. I'll try the changes and post my results.