Class: Ajax

Drupal. Ajax

new Drupal.Ajax(base, element, element_settings)

Ajax constructor.

The Ajax request returns an array of commands encoded in JSON, which is then executed to make any changes that are necessary to the page.

Drupal uses this file to enhance form elements with #ajax['url'] and #ajax['wrapper'] properties. If set, this file will automatically be included to provide Ajax capabilities.

Name Type Description
base string optional

Base parameter of Drupal.Ajax constructor

element HTMLElement optional

Element parameter of Drupal.Ajax constructor, element on which event listeners will be bound.

element_settings Drupal.Ajax~element_settings

Settings for this Ajax object.

Members

static,constantDrupal.Ajax.AJAX_REQUEST_PARAMETERstring

Request parameter to indicate that a request is a Drupal Ajax request.

Default Value:
  • _drupal_ajax

$formjQuery

elementHTMLElement

instanceIndexbool number

optionsobject

Options for the jQuery.ajax function.

Properties:
Name Type Default Description
url string

Ajax URL to be called.

data object

Ajax payload.

beforeSerialize function

Implement jQuery beforeSerialize function to call Drupal.Ajax#beforeSerialize.

beforeSubmit function

Implement jQuery beforeSubmit function to call Drupal.Ajax#beforeSubmit.

beforeSend function

Implement jQuery beforeSend function to call Drupal.Ajax#beforeSend.

success function

Implement jQuery success function to call Drupal.Ajax#success.

complete function

Implement jQuery success function to clean up ajax state and trigger an error if needed.

dataType string 'json'

Type of the response expected.

type string 'POST'

HTTP method to use for the Ajax request.

urlstring

Processed Ajax URL.

wrapperstring

Methods

beforeSend(xmlhttprequest, options)

Prepare the Ajax request before it is sent.

Name Type Description
xmlhttprequest XMLHttpRequest

Native Ajax object.

options object

jQuery.ajax options.

beforeSerialize(element, options)

Handler for the form serialization.

Runs before the beforeSend() handler (see below), and unlike that one, runs before field data is collected.

Name Type Description
element object optional

Ajax object's element_settings.

options object

jQuery.ajax options.

beforeSubmit(form_values, element, options)

Modify form values prior to form submission.

Name Type Description
form_values Array.<object>

Processed form values.

element jQuery

The form node as a jQuery object.

options object

jQuery.ajax options.

error(xmlhttprequest, uri, customMessage)

Handler for the form redirection error.

Name Type Description
xmlhttprequest object

Native XMLHttpRequest object.

uri string

Ajax Request URI.

customMessage string optional

Extra message to print with the Ajax error.

eventResponse(element, event)

Handle an event that triggers an Ajax response.

When an event that triggers an Ajax response happens, this method will perform the actual Ajax call. It is bound to the event using bind() in the constructor, and it uses the options specified on the Ajax object.

Name Type Description
element HTMLElement

Element the event was triggered on.

event jQuery.Event

Triggered event.

execute(){object}

Execute the ajax request.

Allows developers to execute an Ajax request manually without specifying an event to respond to.

Returns:
Type Description
object Returns the jQuery.Deferred object underlying the Ajax request. If pre-serialization fails, the Deferred will be returned in the rejected state.

getEffect(response){object}

Build an effect object to apply an effect when adding new HTML.

Name Type Description
response object

Drupal Ajax response.

Name Type Description
effect string optional

Override the default value of Drupal.Ajax#element_settings.

speed string | number optional

Override the default value of Drupal.Ajax#element_settings.

Returns:
Type Description
object Returns an object with showEffect, hideEffect and showSpeed properties.

keypressResponse(element, event)

Handle a key press.

The Ajax object will, if instructed, bind to a key press response. This will test to see if the key press is valid to trigger this event and if it is, trigger it for us and prevent other keypresses from triggering. In this case we're handling RETURN and SPACEBAR keypresses (event codes 13 and 32. RETURN is often used to submit a form when in a textfield, and SPACE is often used to activate an element without submitting.

Name Type Description
element HTMLElement

Element the event was triggered on.

event jQuery.Event

Triggered event.

setProgressIndicatorBar()

Sets the progress bar progress indicator.

setProgressIndicatorFullscreen()

Sets the fullscreen progress indicator.

setProgressIndicatorThrobber()

Sets the throbber progress indicator.

success(response, status)

Handler for the form redirection completion.

Name Type Description
response Array.<Drupal.AjaxCommands~commandDefinition>

Drupal Ajax response.

status number

XMLHttpRequest status.

Type Definitions

element_settingsobject

Settings for an Ajax object.

Properties:
Name Type Argument Default Description
url string

Target of the Ajax request.

event string <optional>
<nullable>

Event bound to settings.element which will trigger the Ajax request.

keypress bool <optional>
true

Triggers a request on keypress events.

selector string <nullable>

jQuery selector targeting the element to bind events to or used with Drupal.AjaxCommands.

effect string <optional>
'none'

Name of the jQuery method to use for displaying new Ajax content.

speed string | number <optional>
'none'

Speed with which to apply the effect.

method string <optional>

Name of the jQuery method used to insert new content in the targeted element.

progress object <optional>

Settings for the display of a user-friendly loader.

Properties
Name Type Argument Default Description
type string <optional>
'throbber'

Type of progress element, core provides 'bar', 'throbber' and 'fullscreen'.

message string <optional>
Drupal.t('Please wait...')

Custom message to be used with the bar indicator.

submit object <optional>

Extra data to be sent with the Ajax request.

Properties
Name Type Argument Default Description
js bool <optional>
true

Allows the PHP side to know this comes from an Ajax request.

dialog object <optional>

Options for Drupal.dialog.

dialogType string <optional>

One of 'modal' or 'dialog'.

prevent string <optional>

List of events on which to stop default action and stop propagation.