function 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

1 string reference to 'ajax_example_render_link'
ajax_example_menu in ajax_example/ajax_example.module
Implements hook_menu().

File

ajax_example/ajax_example_misc.inc, line 22

Code

function ajax_example_render_link() {
    // drupal_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.
    drupal_add_library('system', 'drupal.ajax');
    $explanation = t("\nThe link below has the <i>use-ajax</i> class applied to it, so if\njavascript is enabled, ajax.js will try to submit it via an AJAX call instead\nof a normal page load. The URL also contains the '/nojs/' magic string, which\nis stripped if javascript is enabled, allowing the server code to tell by the\nURL 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;
}