drupal_add_js - working with inline but not file I have the following code which works:

&lt;?php
function mymodulename_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if($form_id == 'job_node_form') {
    drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
  }
}
?&gt;


but the following doesn't:

&lt;?php
function mymodulename_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if($form_id == 'job_node_form') {
    drupal_add_js(drupal_get_path('module','conflict') . '/js/test.js');
  }
}
?&gt;


The code in sites/all/modules/mymodulename/js/test.js is:

If(Drupal.jsEnabled) {
  $(document).ready(function() {
    alert("Hello!");
  });
}


And when I look in the page source when I'm in "job_node_form", I can see the js file loaded.

Edit:

I've found that:

$(document).ready(function()


is causing the issue. I've tried both $ and jQuery.

Is the inline code timed differently to the code in a js file? I'm wondering if the code in the js file runs before all DOM objects are loaded?
With Drupal instead of using $(document).ready, you can/should use Drupal.behaviors. Here is a boilerplate JavaScript wrapper you can use:

(function($, Drupal) {

  // Custom functions and variables can go here

  Drupal.behaviors.moduleName = {
    attach:function() {
      // Put your document's onload code here
    }
  };
}(jQuery, Drupal));


By the way, now that it's all done, to go back to the original topic, I wouldn't/don't use drupal_add_js() at all. Instead, you can do this:

&lt;?php
if($form_id == 'job_node_form') {
  $form['#attached']['js'][] = array(
    'type' =&gt; 'file',
    'data' =&gt; drupal_get_path('module','conflict') . '/js/test.js',
  );
}
?&gt;


This attaches the file directly to the form, allowing it to be overridden further down the line through the context of the form it belongs to.