By cyprien2 on
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
Debug
Try to debug with firebug or aptana studio. Helps me a lot...
-------------------------------
Read jQuery howto's and tips at jQuery HowTo Resouce
I've tried that, but doesn't
I've tried that, but doesn't help me... No one have tried this function ?
So what is it saying?
So what is it saying?
There is no 'myvarname' variable in Drupal.settings?
-------------------------------
Read jQuery howto's and tips at jQuery HowTo Resouce
Had the same problem. This is
Had the same problem.
This is how it worked for me:
following way works for me...
$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.
following way works for me...
$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.