Sending Parameters or Settings to Javascript in Drupal 7

By Anonymous (not verified), 6 March, 2012

drupal

 

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:

 

[prism:php] $settings = array ( 'foo' => 'bar', 'name' => 'john', ); drupal_add_js( array ("my_module" => $settings), 'setting'); [/prism:php] 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:

 

[prism:php] var name = Drupal.settings['my_module']['name']; [/prism:php]

 

What is Drupal?

 

Drupal is one of the most versatile CMS today and the one used to build many of the web pages that we visit every day. According to the definition given on its official website, “Drupal is a content management software. It is used to create many of the websites and applications of daily use”.

 

With a flexible and modular system, Drupal has managed to penetrate the taste of developers when they must design a web page that is easy to use for the common user.

 

We recommend you on video

 

Content
Title
Let's work together!
Text Alignment
Left
Webform
Margin
With Large margin top
Thumbnail
Image
Weight
0
Hero
Title
Sending Parameters or Settings to Javascript in Drupal 7
Image
Image
Text Color
White
Text Alignment
Left
Size
Medium
Overlay effect
Hide overlay effect
Date
Sidebar
CTA
Premium
No