I have the following code which works:
<?php
function mymodulename_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'job_node_form') {
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
}
}
?>
but the following doesn't:
<?php
function mymodulename_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'job_node_form') {
drupal_add_js(drupal_get_path('module','conflict') . '/js/test.js');
}
}
?>
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?
jsEnabled
not defened yet, moreover if browser unsupports js, whole code will not able to execute. Try thisjQuery(document).ready(function($) { alert('123'); });
in your file or try$form['#attached']['js'][] = array('data' => 'path_to_js_file.js', 'type' => 'file');
– Oleg Sherbakov Jul 9 '13 at 20:15