1-866-277-9958

Expose server side data from your module for client side javascript use

Adding the following code in any PHP area in Drupal will give you settings values available client-side that you can use when writing JavaScript code.

Pass a key/value associative array to drupal_add_js() and give it type value of 'setting'. Note that I am using an array of arrays with the key of the first array as the name of the module. This is to namespace the settings. This will keep your settings from overriding others and others from overriding yours.

//PHP code in your module or in a PHP snippet anywhere on your site
 
$jquery_settings = array(
  'myModule' => array(
    'mySetting1' => 'myvalue1', 
    'mySetting2' => 'myvalue2' )
);
drupal_add_js($jquery_settings, 'setting');

Now, within your JavaScript you can access these settings that you defined server-side

/**
*  My JavaScript File
*/
alert(Drupal.settings.myModule.mySetting1);
// displays myvalue1


Get Drupal help when you need it most! Find hundreds of great tutorials. Track, rate, comment and more. Create Account

Hi

JavaScript - a scripting language most often used to create scenarios of your browser, built-in web stranitsy.JavaScript has a number of properties of object-oriented language, but thanks to support prototyping of objects in it differs from traditional GS Languages.JavaScript code is included in the HTML-code pages and turns the interpreter into the browser. JavaScript is currently fully occupies a niche browser languages, this interesting information about JavaScript, I found the site http://www.queentorrent.com.

Syndicate content

©1999 - 2011 LevelTen Interactive - Dallas, TX