How do I properly pass values from PHP to JavaScript? I am currently writing a module and I am trying to pass some variables to the Drupal.settings.

My module uses the following code.

function bcc_preprocess_views_view(&amp;$vars) {
    drupal_add_js(array('modulname' =&gt; array('key' =&gt; 'value')), 'setting');
    drupal_add_js(drupal_get_path('module', 'modulname') .'/script.js');
}


The script.js content is the following.

(function ($) {
    console.log(Drupal.settings.modulname);
})( jQuery );


The output I find in the log is undefined, but when I check the content of  Drupal.settings.modulname from the browser, it gives me the desired value.

When I add this code, the log shows the right values.

drupal_add_js('jQuery(document).ready(function({console.log(Drupal.settings.royal)});', 'inline');


What is wrong with that? What is the correct code to use?
You first JS snippet executes before the page is done loading.  Is it not the same syntax as the shorthand for declaring the document.onready event handler.

You working example runs from an onready, but the Drupal best practice is to use a behavior, eg:

(function ($, Drupal, undefined) {
  Drupal.behaviors.mymodule = {
    attach: function (context, settings) {
      console.log(settings.mymodule);
    }
  };
})(jQuery, Drupal);


Take a look at Managing JavaScript in Drupal 7 for more information.