Tell me more ×
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
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

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

3 Answers

up vote 19 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.setting.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

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

You can just have php output a javascript global variable. For example:

<html>
<head>
<script>
<?
   $globVal = 1234;
   echo "var globVal=" . $globVal . ";\n";
?>
</script>
</head>
<body>
</body>
</html>

Any javascript files loaded at this time should have access to this value, either under globVal, or, if you're in a different context, window.globVal.

One thing to keep in mind... depending on where your other js files are loaded, they COULD try to access this variable before the above script line is executed. If you use jquery, you'd need to wrap in a jQuery(document).ready() event.

Good luck!

share|improve this answer
Thank you for your response. As far as i understand you suggest to add another java script in the .php.tpl, in which to do the assignment of global variable and then use this global variable in my remote .js? – alexey May 11 '11 at 13:27
1  
Without proper sanitization, this will, sooner or later, lead to JavaScript syntax error (as soon as $globVal contains a ") – Pierre Buyle May 11 '11 at 14:15
Er ... except that globVal is an integer. – btx May 12 '11 at 4:06

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.