Reply to comment

For example we want to add an api key as a variable to our jquery script. In drupal we define our apikey using a settings form. We add the js using drupal_add_js. Notice that the second parameter says 'setting', this is used to tell the drupal javascript system to add the array as a setting in scripts. The Jquery object is allready extended with Drupal.settings and this is where our setting will be available.
To avoid namespace clashes it is good practice to put variables in arrays by module.

<?php
function module_name_settings() {
  
$form['module_name_apikey'] = array(
       
'#type'           => 'textfield',
       
'#title'          => t('Google api key'),
       
'#default_value'  => variable_get('module_name_apikey'''),
       
'#size'           => 60,
       
'#weight' => -5,
       
'#description'    => l(t('Get your google api key here'), 'http://code.google.com/apis/maps/signup.html'),
  );

  
system_settings_form($form);
}

//Implementation of hook init()
function module_name_init() {
  
drupal_add_js(
    array(
      
'module_name' => array(
        
'apikey' => variable_get('module_name_apikey'''),
      )
    ),
    
'setting'
  
);
}
?>

In your jquery script the variable will be available in Drupal.settings

var apikey = Drupal.settings.module_name.apikey;
//To view output in firebug
console.log(apikey);
//If you dont use firebug
alert(apikey);

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><img><p><b><i><table><th><tr><td><blockquote><br /><img /><tbody><span><strike>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <codes>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.