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?
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