Contents
How to add a JS file to Drupal?
The jquery.js file is included automatically so you don’t need to worry about adding it. To add JS (or CSS) file from form builder function the recommended way is to use Form API #attached property as below, See Form API reference for more details. (drupal_add_js is deprecated in Drupal 8; use ‘#attached’ instead.)
Why does Drupal not load CSS on every page?
Drupal uses a high-level principle: assets (CSS or JS) are still only loaded if you tell Drupal it should load them. Drupal does not load all assets on every page because it slows down front-end performance.
How to add jQuery to a Drupal theme?
If your theme requires jQuery or other assets you want to load on all pages, add them in *. libraries. yml. and then include library in the THEME. info. yml. In Drupal 7 libraries had to be defined using hook_library_info().
How to define a library in Drupal 7?
In Drupal 7 libraries had to be defined using hook_library_info(). That has been replaced with *. libraries. yml file. Save the CSS or JS to a file using the proper naming conventions and file structure. Define a “library”, which registers these CSS/JS files with your theme.
What does it mean to add external JavaScript in Drupal?
Add external JavaScript (‘external’): Allows the inclusion of external JavaScript files that are not hosted on the local server. Note that these external JavaScript references do not get aggregated when preprocessing is on. Add settings (‘setting’): Adds settings to Drupal’s global storage of JavaScript settings.
When to use inline JavaScript in Drupal 7?
Add inline JavaScript code (‘inline’): Executes a piece of JavaScript code on the current page by placing the code directly in the page (for example, to tell the user that a new message arrived, by opening a pop up, alert box, etc.). This should only be used for JavaScript that cannot be executed from a file.