2

The title is pretty self-explanatory, I would like to do something like this to display a jQuery UI dialog:

function my_ajax_page_callback() {
  drupal_add_library('system', 'ui.dialog');
  drupal_add_js('jQuery.dialog(...)','inline');
}

Specifically, I would like to include the library on-demand, not on every non-ajax request. Is that possible in D7?

2 Answers 2

3

drupal_add_library() under the hood ends up putting things into the same array as drupal_add_js() and drupal_add_css().

These get called by ajax_render(), so your function should work (so long as you are using ajax_render).

However this will add the library load to every ajax request. This will slow them down some what, and most of the item you are using ajax for speed so that may not be such a good idea. Your html JS should be cached and it should only include the ui.dialog on pages where you need it. So I think you may be better off using the standard JS caching.

In simple cases you may be able to use ajax_command_alert() in stead.

1

I do not think so. You will have to do it in this way....

$script = '
  <script type="text/javascript">
    <!--//--><![CDATA[//><!--
    (function($){
      $("head").append("<link>");
      css = $("head").children(":last");
      css.attr({
         rel:  "stylesheet",
         type: "text/css",
         href: "/path/to/style.css"
      });
      $.getScript("/path/to/script.js");
    })(jQuery);
    //--><!]]>
  </script>
';

Now make the $script part of ajax output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.