Trouble passing PHP variables to JS using "attach" Module-noob here. I've seen plenty of threads on this topic in general but haven't found any concerning use of "attach." I'm working on a fairly simple module that applies some JavaScript to every page on the site. It was working fine, but pareview told me not to use drupal_add_js() in hook_page_build(), instructing me to use attach instead. I can't figure out how to get my PHP variables over to my JS using this method.

The JS file itself is loading, but it is not receiving the data from the PHP variables. From what I have read, I understand that the following is the way to accomplish what I am trying to do, but obviously I've got something wrong somewhere. Here is my code:

mymodule.module:

function mymodule_page_build() {

  $page['#attached']['js'] = array(
    array(
      'data' =&gt; drupal_get_path('module', 'mymodule') . '/mymodule.js',
      'options' =&gt; array(
        'preprocess' =&gt; TRUE,
        'every_page' =&gt; TRUE,
      ),
    ),
    array(
      'data' =&gt; array(
        'mymodule_status' =&gt; variable_get('mymodule_status', 0),
      ),' =&gt; 'setting',
    ),
  );
}


mymodule.js:

(function ($) {
  "use strict";
  Drupal.behaviors.myModule = {
    attach: function (context, settings) {
      var moduleStatus = Drupal.settings.mymodule.mymodule_status;
      var milk = 'milk';
      $(document).ready(function () {
        // Debug:
        console.log('first');      // works
        console.log(milk);         // works
        console.log(moduleStatus); // Says mymodule is undefined
    }
  };
})(jQuery);


Thank you for any help you can offer.
Have a look on this page

For getting the value of a variable that has been set in the module you have to use like this Drupal.settings.&lt;modulename&gt;.&lt;variablename&gt;

In your module file 

function mymodule_page_build() {
  $status = variable_get('mymodule_status', 0);
  drupal_add_js(array('MODULENAME' =&gt; array('mymodule_status' =&gt; $status)), 'setting'); 

  $page['#attached']['js'] = array(
    array(
      'data' =&gt; drupal_get_path('module', 'mymodule') . '/mymodule.js',
      'options' =&gt; array(
        'preprocess' =&gt; TRUE,
        'every_page' =&gt; TRUE,
      ),
    ),
  );
}


So in your case it should be

Drupal.settings.mymodule.mymodule_status