See #1959434: Empty "forms" settings for first form breaks validation for some background info

Core issues that should have fixed this #561858: [Tests added] Fix drupal_add_js() and drupal_add_css() to work for AJAX requests too by adding lazy-load to AJAX framework, but the test doesn't check if the javascripts gets actually added to the page, the files added using drupal_add_js are passed to the calling page, but they never get added to the page

new_content_wrapped only contains a line break for each added file.

This is probably a jQuery problem:
jQuery('<div></div>').html('<script src="/misc/ajax.js" />') executed in a console loads the file (jquery.js:142), but the div is empty.

If you do the same on http://api.jquery.com/html/ the script isn't loaded, but the div will contain the script tag.

Comments

attiks’s picture

Title: Scripts added usiing drupal_add_js() are never added to the page after an AJAX callback » Scripts added using drupal_add_js() are never added to the page after an AJAX callback
Issue tags: +JavaScript, +Needs manual testing
attiks’s picture

Assigned: Unassigned » attiks
Priority: Major » Normal

I stand corrected, it is working, but the scripts are never added to the DOM, they are loaded and executed by jquery.

attiks’s picture

Status: Active » Closed (works as designed)

Jelle_S checked with jQuery 1.7 and it works as well

Wim Leers’s picture

#2: exactly, this is jQuery <=1.9 being extremely retarded counterintuitive (yes, I've lost many hours because of this extremely weird behavior).

This also causes CKEditor to break when it's lazy loaded, and it's why the patch at #1894644: Unidirectional editor configuration -> filter settings syncing needs to do this:

+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function ckeditor_form_filter_admin_format_form_alter(&$form, &$form_state) {
+  // @todo D8 remove this when we upgrade to jQuery >=1.9: jQuery <1.9 breaks
+  // CKEditor when CKEditor is dynamically loaded, because jQuery <1.9 hides
+  // <script> tags.
+  // This cannot live in ckeditor.module, because it must already exist
+  // *before* ckeditor.js is dynamically loaded, and AjaxResponse does not yet
+  // support inline CSS/JS, so moving this into the 'ckeditor' library also
+  // does not work.
+  // @see http://bugs.jquery.com/ticket/11795#comment:20
+  $form['editor']['editor']['#attached']['js'][] = array(
+    'type' => 'inline',
+    'scope' => 'header',
+    'data' => 'window.CKEDITOR_BASEPATH="' . url('core/misc/ckeditor/', array('absolute' => TRUE)) . '";',
+  );
+}

I.e. relevant jQuery ticket: http://bugs.jquery.com/ticket/11795#comment:20

attiks’s picture

Wim thanks for the background info.

Wim Leers’s picture

np :)

rjacobs’s picture

Issue summary: View changes

Alright, so I'm sorry to be posting in this old closed issue, but several different pathways through other issue queues keep routing me back to this one. I'm doubtful I have a topic for a new issue, and I may just need some simple clarification on the points above...

As best I can tell, scripts added using drupal_add_js(), or #attached['js'](which of course just uses the former), are still not always added to the page after an AJAX callback, regardless of the jQuery version (I've tried 1.4, 1.5, 1.7, 1.10).

I'm suspect that the previous issues that have addressed this are making other assumptions that I'm just totally missing. Is is possible that things only work for elements that are explicitly declared as part of the FAPI and use the appropriate jQuery version? One of my cases involves a custom display formatter that adds some dynamically calculated inline js via #attached (i.e., it's not tied to a formal form element). That js is still never added to the DOM when the formatter is used to (re)render content as part of an AJAX response. Again, updating jQuery does not seem to have any effect.

Is there an assumption I'm missing about the baseline cases where drupal_add_js() is actually supported in an AJAX response?

rjacobs’s picture

Re. #7, never mind. I think I've reversed-engineered enough of the history of this issue to see that there are indeed some important assumptions required for drupal_add_js() to work inside AJAX. I've decided to post notes in the previous/main issue about this instead, as it seems more appropriate there for ongoing refernce - #561858: [Tests added] Fix drupal_add_js() and drupal_add_css() to work for AJAX requests too by adding lazy-load to AJAX framework