Hi,
I want to get a variable (path_to_theme()) from my javascript file.
So in my page.tpl.php, i've put in the HEAD section :

  drupal_add_js(array('myvarname' => 'test'), 'setting');

and in the body section :

var test="town"; document.write(test); document.write(Drupal.settings.myvarname);

It writes : town undefined
Where i'm wrong ?
Thanx in advance.

Comments

uzbekjon’s picture

Try to debug with firebug or aptana studio. Helps me a lot...

-------------------------------
Read jQuery howto's and tips at jQuery HowTo Resouce

cyprien2’s picture

I've tried that, but doesn't help me... No one have tried this function ?

uzbekjon’s picture

So what is it saying?

There is no 'myvarname' variable in Drupal.settings?

-------------------------------
Read jQuery howto's and tips at jQuery HowTo Resouce

fxarte’s picture

Had the same problem.
This is how it worked for me:

$(document).ready(function() { // Clearly when the document is ready to play with :)
  alert(Drupal.settings.myvarname);
});
chandravilas’s picture

$phpdata = "php data to jQuery";

drupal_add_js(array('My_Module_name' => $phpdata), 'setting');

<script>

(function ($) {

Drupal.behaviors.My_Module_name = {

attach: function (context, settings) {

var data = settings.My_Module_name;

console.log(data);

}

};

})(jQuery);

<script>

Note: but it will work in version 7 and not in case of version 8.

chandravilas’s picture

$phpdata = "php data to jQuery";

drupal_add_js(array('My_Module_name' => $phpdata), 'setting');

<script>

(function ($) {

Drupal.behaviors.My_Module_name = {

attach: function (context, settings) {

var data = settings.My_Module_name;

console.log(data);

}

};

})(jQuery);

<script>

Note: but it will work in version 7 and not in case of version 8.