By davebv on
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
find answer yet?
I have same problem. If you find the answer after 3 years, can you post it? Thanks lot
Show your code.
Show your code.
Contact me to contract me for D7 -> D10/11 migrations.
Same Problem: Drupal.settings.mytheme is undefined (js error)
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.jsIn 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.)
In my script.js file I have the following code
The error: Drupal.settings.mytheme is undefined is thrown.
I have also tried the following in my template.php file:
and the following in my script.js file:
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!
It may be because you aren't
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:
Contact me to contract me for D7 -> D10/11 migrations.
Thanks Jay. Sorry about the
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.