3

I adjusted your code to match the one from the docs. MYMODULE/js/myscripts.js: (function ($, Drupal) { Drupal.behaviors.myModuleBehavior = { attach: function (context, settings) { $('.block-facets-ajax', context).once('myModuleBehavior').each(function () { // Apply the myModuleBehavior effect to the elements only once. $(this)....


3

for me that submits the node: (function (window, Drupal, $) { Drupal.behaviors.drafter = { attach: function attach(context) { var button = $('#edit-submit'); var newbutton = button.clone().prop('id', 'edit-submit-drafter'); var confirmationDialog = Drupal.dialog('#drafter-elements', { resizable: false, closeOnEscape: ...


3

once is a jQuery plugin; it doesn't really make sense to mix it with vanilla JS event handlers, as you're already using jQuery by that point, and it's easier to attach events with jQuery. After you've refactored your code to use jQuery, its usage is well explained, with examples, in the Drupal javascript docs. There are many permutations, here is one ...


2

I solved the problem using drupal_add_html like this, $data = array( '#tag' => 'script', '#value' => '', '#attributes' => array( 'src' => 'https://code.jquery.com/jquery-3.4.1.js', 'integrity' => 'sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=', ), ); drupal_add_html_head($data, 'unique_key');


2

once() may work in many cases. I've also used a flag like: (function ($, Drupal) { let flag = false; Drupal.behaviors.my_module = { attach: function (context, settings) { if (flag === false) { flag = true; console.log('This should only display one time.'); } } }; })(jQuery, Drupal); Please note that I only wrote ...


1

99 % sure it is the way your js file is set. as the above should work. for a test please do this: (function ($, Drupal, drupalSettings) { 'use strict'; Drupal.behaviors.testStuff = { attach: function (context) { alert('yes i am attached'); console.log(drupalSettings); if (drupalSettings.example) { console.log(...


1

For a specific page or content type, do: /** * Implements hook_page_attachments(). */ function MYMODULE_page_attachments(array &$attachments) { if ($node = \Drupal::routeMatch()->getParameter('node')) { if ($node->getType() === 'book') { // You can also check on certain ID's here as well. // Current page is a Book node. $...


1

Define an asset library in mymodule.libraries.yml. An asset library can declare either CSS assets or JavaScript assets, or both. It can also declare dependencies on other asset libraries. For example on jQuery (core/jquery) or perhaps your JavaScript defines a "Drupal behavior", in which case you depend on the supporting infrastructure for that (core/drupal)....


1

Another option is to use underscore's once function. # your_theme_or_module.libraries.yml global-styling: js: js/main.js: {} dependencies: - core/underscore // js/main.js (function ($, _, Drupal) { "use strict"; Drupal.behaviors.example = { attach: _.once(function(context, settings) { // do stuff once. }) } })(jQuery, _,...


1

You could set a global variable and then set it in your wakeUpAndRemember function var do_once = false; function wakeUpAndRemember() { console.log('test'); do_once = true; } Drupal.behaviors.recherche = { attach: function (context, settings) { if (do_once == false){ wakeUpAndRemember(); } } }


1

I don't think this is possible using .once(), but you can do it by setting a variable in your function and only running the function if that variable is not set. Your function shall look like this: var wakeUpAndRemember = (function(mapObj) { var executed = false; return function() { if (!executed) { executed = true; // The code from ...


1

You can do this in hook_preprocess_html(): function theme_preprocess_html(&$var) { $script = [ '#tag' => 'script', '#attributes' => [ 'type' => 'module', 'src' => 'path/to/script.js', ], '#value' => '', ]; drupal_add_html_head($script, 'script'); }


1

Bit old, but for module version 1, you have to use OwlCarousel 1 https://github.com/InchoonPark/meteor-owl-carousel-1/


Only top voted, non community-wiki answers of a minimum length are eligible