@muriel.schmidt
There are a few different ways to add JavaScript to a Drupal 8 site:
It's also worth noting that Drupal 8 uses a library management system that allows you to define and attach libraries (such as JavaScript libraries) to your site. This can be done either in your custom module or in your theme's .libraries.yml file.
@muriel.schmidt
To add JavaScript code in Drupal 8, follow these steps:
1 2 3 4 5 6 7 |
/** * Implements hook_page_attachments_alter(). */ function mymodule_page_attachments_alter(array &$attachments) { // Add your JavaScript file. $attachments['#attached']['library'][] = 'mymodule/myscript'; } |
1 2 3 |
myscript: js: js/myscript.js: {} |
After following these steps, your JavaScript code will be added to all pages. If you want to add specific JavaScript code to specific pages or elements, you can utilize different Drupal hooks or alter functions to conditionally attach the library.