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?

up vote 8 down vote accepted

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

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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