Scroll
Drupal 8 JS behaviors
Drupal 8 JS behaviors
Drupal 8 JS behaviors
1 answer
votes: 1373
Answer
Drupal behaviors (Drupal.behaviors
) are still part of javascript in core. These behaviors will be executed on every request, including AJAX requests.
Don't forget to add a dependency to
core/drupal
in order to use Behaviors.
Below is an example of a Drupal Javascript Behavior.
(function ($) {
'use strict';
Drupal.behaviors.awesome = {
attach: function(context, settings) {
$('main', context).once('awesome').append('<p>Hello world</p>');
}
};
}(jQuery));