5

I am using Drupal 7.

In the template file page.tpl.php there is a variable called $message which prints the messages like error, warning etc.

I want to access this variable in a JavaScript file. How can I do so?

8

In your page.tpl.php file:

$my_settings = array(
  'message' => $message,
);

drupal_add_js(array('my_settings' => $my_settings), 'setting');

Then in your JavaScript file you can access $message variable like this:

var message = Drupal.settings.my_settings.message;

Reference: The Drupal JavaScript API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.