function ajax_example_render_link_ra in Examples for Developers 7
AJAX-enabled link in a renderable array.
Demonstrates a clickable AJAX-enabled link using a renderable array with the #ajax property.
A link that is constructed as a renderable array can have the #ajax property, which ensures that the link submission is done without a page refresh. The href of the link is used as the ajax callback, but it degrades gracefully without JavaScript because if the 'nojs' portion of the href is not stripped out by js, the callback will return content as required for a full page reload.
The necessary JavaScript file, ajax.js, will be included on the page automatically.
Return value
array Form API array.
1 string reference to 'ajax_example_render_link_ra'
- ajax_example_menu in ajax_example/
ajax_example.module - Implements hook_menu().
File
- ajax_example/
ajax_example_misc.inc, line 61 - AJAX Miscellaneous Topics.
Code
function ajax_example_render_link_ra() {
$explanation = "\nThe link below has been rendered as an element with the #ajax property, 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.";
$build['my_div'] = array(
'#markup' => $explanation . '<div id="myDiv"></div>',
);
$build['ajax_link'] = array(
'#type' => 'link',
'#title' => t('Click here'),
// Note the /nojs portion of the href - if javascript is enabled,
// this part will be stripped from the path before it is called.
'#href' => 'ajax_link_callback/nojs/',
'#id' => 'ajax_link',
'#ajax' => array(
'wrapper' => 'myDiv',
'method' => 'html',
),
);
return $build;
}