Skip to main content
added 1 characters in body
Source Link
Gnuget
  • 1.1k
  • 6
  • 14

Drupal have a javascript array for save settings and variables, take a look here:

http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/6

Check the "settings" option in that page.

A example could be:

$my_module_variables = array(
  "url"  => "http://google.com",
  "name" => "this is a variable"
);

drupal_add_js(array("my_module_vars" => $my_module_variables),'settings'settings');

And for use it in your javascript files you can do:

console.log(drupal.settings.my_module_vars.url);
//will print http://google.com
console.log(drupal.settings.my_module_vars.name);
//will print "this is a variable"

In this way you can set javascript vars without need print javascript code, only using a php array.

Drupal have a javascript array for save settings and variables, take a look here:

http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/6

Check the "settings" option in that page.

A example could be:

$my_module_variables = array(
  "url"  => "http://google.com",
  "name" => "this is a variable"
);

drupal_add_js(array("my_module_vars" => $my_module_variables),'settings);

And for use it in your javascript files you can do:

console.log(drupal.settings.my_module_vars.url);
//will print http://google.com
console.log(drupal.settings.my_module_vars.name);
//will print "this is a variable"

In this way you can set javascript vars without need print javascript code, only using a php array.

Drupal have a javascript array for save settings and variables, take a look here:

http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/6

Check the "settings" option in that page.

A example could be:

$my_module_variables = array(
  "url"  => "http://google.com",
  "name" => "this is a variable"
);

drupal_add_js(array("my_module_vars" => $my_module_variables),'settings');

And for use it in your javascript files you can do:

console.log(drupal.settings.my_module_vars.url);
//will print http://google.com
console.log(drupal.settings.my_module_vars.name);
//will print "this is a variable"

In this way you can set javascript vars without need print javascript code, only using a php array.

Source Link
Gnuget
  • 1.1k
  • 6
  • 14

Drupal have a javascript array for save settings and variables, take a look here:

http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/6

Check the "settings" option in that page.

A example could be:

$my_module_variables = array(
  "url"  => "http://google.com",
  "name" => "this is a variable"
);

drupal_add_js(array("my_module_vars" => $my_module_variables),'settings);

And for use it in your javascript files you can do:

console.log(drupal.settings.my_module_vars.url);
//will print http://google.com
console.log(drupal.settings.my_module_vars.name);
//will print "this is a variable"

In this way you can set javascript vars without need print javascript code, only using a php array.