Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I hope someone is able to help me. The problem is the following one: 1)I have module in which a assign the variable in order to send to the template file php.tpl

<?php
...
$testvar="Hello from alex!";
$variables['testvar'] = $testvar; 
...
?>

This variable can be shown in the php.tpl file like

<?php print $testvar?>

2)I have separated .js file How can i get access to this varuiable inside .js file?

I know how it'll be if .js file is inside .php.tpl:

<?php
$testvar="Hello from alex!";
?>
<script type="text/javascript">
var myVar = '<?php print $testvar?>';
</script>

The problem is how to do the same if these two files .js and .php.tpl are separated?

share|improve this question

migrated from stackoverflow.com Jun 1 '11 at 14:07

This question came from our site for professional and enthusiast programmers.

2  
don't blindly echo out a variable into Javascript. If there's any Javascript metacharacters (single quotes, especially), you'll introduce syntax errors. Do var myVar = <?php echo json_encode($testvar) ?>; which will take care of any such issues for you, regardless of the PHP var's data-type/content. –  Marc B May 11 '11 at 14:17

2 Answers 2

up vote 34 down vote accepted

You should use drupal_add_js() in your module, there is no need to output the variable in your .tpl.php:

drupal_add_js(array('YOURMODULE' => array('testvar' => $testvar)), array('type' => 'setting'));

And in your JavaScript, you can the access the value in Drupal.settings.YOURMODULE.testvar:

alert(Drupal.settings.YOURMODULE.testvar);

Direct usage of global variables (as suggested in your code sample) is a discouraged practice in JavaScript as it clutter the global namespace. Also, if your code is triggered on page load, check the "Behaviors" section in Managing JavaScript in Drupal 7 documention (the whole page is worth reading).

share|improve this answer
    
It doesn't work! the error: Drupal.setting is undefined. What's wrong with it? –  alexey May 14 '11 at 15:52
3  
Sorry, it's Drupal.settings with the S. –  Pierre Buyle May 16 '11 at 7:08
    
Thanks for your explanation! –  Heihachi Mar 8 '14 at 16:00
    
Also how would you do the same for passing arrays to javascript? –  pal4life May 28 at 23:44
    
Arrays with consecutive are encoded as JavaScript arrays. All the settings collected via drupal_add_js() will be encoded in JSON using json_encode($var, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); –  Pierre Buyle May 29 at 11:26

In your MODULENAME.module file:

$testVariable = 'himanshu';
drupal_add_js(array('MODULENAME' => array('testvar' => $testVariable)), array('type' => 'setting'));
drupal_add_js(drupal_get_path('module', 'MODULENAME') . '/MODULENAME.js');

In MODULENAME.js file:

(function($) {
  Drupal.behaviors.MODULENAME = {
    attach: function (context, settings) {
      alert(settings.MODULENAME.testvar);
    }
  };

})(jQuery);

this way you can pass your php variable in drupal javascript file and use the variable :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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