0

I don't know how to run my Drupal.behaviours.mybehaviour on every page load.

Here's my code example that run only once, when I load the page is no longer run my code, how can I change that from once to run on every page load:

       Drupal.behaviors.searchFilter = {
            attach: function (context) {
               once('searchFilter', 'html', context).forEach( function () {
                   console.log("function run");
               });
            }
       }
6
  • Drupal.behaviors is already run on every page load, so it's not clear what you mean.
    – Jaypan
    Sep 13, 2021 at 0:02
  • For example: How can I run console.log("test loading page") display on every page load.
    – mana
    Sep 13, 2021 at 0:10
  • 1
    Drupal.behaviors.searchFilter = { attach: function (context) {console.log("test loading page");}};
    – Jaypan
    Sep 13, 2021 at 0:34
  • @Jaypan thanks for that, how can I attach my library so it get called on every page load. Right now I attached inside my custom module on a checkbox element, but it only execute when the form is submit.
    – mana
    Sep 13, 2021 at 0:46
  • 1
    Drupal.beviours is already called on every page load. So I don't know how to answer your question.
    – Jaypan
    Sep 13, 2021 at 0:53

1 Answer 1

0

I found, you have to attach your library using the hook_page_attachments This hook will attach your library on every page, so it will run on page load.

Then you can execute your js script like the following:

 Drupal.behaviors.searchFilter = {
            attach: function (context) {
                console.log("function run");
            }
       }

Not the answer you're looking for? Browse other questions tagged or ask your own question.