1 ajax_example_misc.inc ajax_example_render_link()

Demonstrates a clickable AJAX-enabled link using the 'use-ajax' class.

Because of the 'use-ajax' class applied here, the link submission is done without a page refresh.

When using the AJAX framework outside the context of a form or a renderable array of type 'link', you have to include ajax.js explicitly.

Return value

array Form API array.

Related topics

File

modules/examples/ajax_example/ajax_example_misc.inc, line 22
AJAX Miscellaneous Topics.

Code

function ajax_example_render_link() {
  // backdrop_add_library is invoked automatically when a form element has the
  // '#ajax' property, but since we are not rendering a form here, we have to
  // do it ourselves.
  backdrop_add_library('system', 'backdrop.ajax');
  $explanation = t("
The link below has the <i>use-ajax</i> class applied to it, so if
javascript is enabled, ajax.js will try to submit it via an AJAX call instead
of a normal page load. The URL also contains the '/nojs/' magic string, which
is stripped if javascript is enabled, allowing the server code to tell by the
URL whether JS was enabled or not, letting it do different things based on that.");
  $output = "<div>" . $explanation . "</div>";
  // The use-ajax class is special, so that the link will call without causing
  // a page reload. Note the /nojs portion of the path - if javascript is
  // enabled, this part will be stripped from the path before it is called.
  $link = l(t('Click here'), 'ajax_link_callback/nojs/', array('attributes' => array('class' => array('use-ajax'))));
  $output .= "<div id='myDiv'></div><div>$link</div>";
  return $output;
}