Sending Parameters or Settings to Javascript in Drupal 7

Imagen de di3gopa
Diego Tejera
Lun, 03/05/2012 - 22:24
Tags: 

There is a simple way to do this in Drupal 7, using the function drupal_add_js and creating an array with the data we want to make accessible:


$settings = array (
    'foo' => 'bar',
    'name' => 'john',
  );

drupal_add_js( array ("my_module" => $settings), 'setting');

Notice that we are using the 'setting' parameter on drupal_add_js, this way we indicate to the function that we are passing settings.

After adding the settings you will be able to access this information on your javascript this way:

  var name = Drupal.settings['my_module']['name'];