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.
In your page.tpl.php file:
$my_settings = array( 'message' => $message, ); drupal_add_js(array('my_settings' => $my_settings), 'setting');
$message variable like this:
var message = Drupal.settings.my_settings.message;