drupal_add_js
drupal_add_js
php:
<?php $my_variables = array('var1' => 'test1', 'var2' => 'test2'); drupal_add_js(array('myModule' => $my_variables), 'setting'); // “myModule”是名称空间,并将防止重复与其他模块. ?>
js:
Drupal.behaviors.myModule = { attach : function() { alert(Drupal.settings.myModule.var1); } }
关联数组:
php:
<?php $my_variables = array('var1' => 'test1', 'var2' => array('var2-1'=>'test2-1')); drupal_add_js(array('myModule' => $my_variables), 'setting'); // “myModule”是名称空间,并将防止重复与其他模块. ?>
js:
Drupal.behaviors.myModule = { attach : function() { alter(Drupal.settings.myModule.var2['var2-1']); } }